Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to place a rectangle in one of my graphs. However it seams that I am mixing dimensions. Is there a way to make sure that \draw uses the same dimension and would fill what is currently enclosed by "estimates"?

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\usepackage{verbatim}

\begin{document}
\centering

\begin{tikzpicture}
\begin{axis}[
            width=0.8\textwidth,
            height=0.4\textwidth,
    ]
    \addplot {-x};
        \addlegendentry{model}
    \addplot coordinates {
        (1,1)
        (1,2)
        (2,2)
        (2,1)
        (1,1)
    };
        \addlegendentry{estimate}
\end{axis}  

    \draw[fill=blue] (1,1) rectangle (2,2);
\end{tikzpicture}

\end{document}
share|improve this question
    
I have also tried moving the $\draw$ inside the $axis$ environment but that's not the solution either. –  MatoBehr Apr 22 at 10:23
    
Here is a help how you can mark your inline code. $...$ should be used only for math mode code. –  Christian Hupfer Apr 22 at 10:28

1 Answer 1

up vote 4 down vote accepted

Place the \draw inside the axis and use the axis cs coordinate system, as in \draw[fill=blue] (axis cs:1,1) rectangle (axis cs:2,2);

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\usepackage{verbatim}

\begin{document}
\centering

\begin{tikzpicture}
\begin{axis}[
            width=0.8\textwidth,
            height=0.4\textwidth,
    ]
    \addplot {-x};
        \addlegendentry{model}
    \addplot coordinates {
        (1,1)
        (1,2)
        (2,2)
        (2,1)
        (1,1)
    };
        \addlegendentry{estimate}
    \draw[fill=blue] (axis cs:1,1) rectangle (axis cs:2,2);
\end{axis}  


\end{tikzpicture}

\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.