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 plot a function with pgfplots and want to combine it with some simple \draw commands from tikz. But to my suprise the coordinate systems seem not to match. The rectangle drawn ranges from x=-10 up to x=10 and from y=0 up to y=1 which as well are the min and max values of the function drawn BUT they do not fit.

How can I force the pgfplots coordinate system and the tikz coordinate system to match and (if somebody knows) what is the problem here?

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[domain=-10:10]

    \begin{axis}[axis lines=none]
        \addplot {exp(x)/(1+exp(x))};
    \end{axis}
    \draw[thick] (-10,0) rectangle (10,1);

\end{tikzpicture}

\end{document}

dings

share|improve this question
4  
If you put the TikZ command inside the axis environment you can use axis coordinate system via (axis cs:-10,0) etc. as coordinates. –  percusse Mar 6 '13 at 15:16
    
which then looks like this \begin{tikzpicture}[domain=-10:10] \begin{axis}[axis lines=none] \addplot {exp(x)/(1+exp(x))}; \draw[thick] (axis cs: -10,0) rectangle (axis cs: 10,1); \end{axis} \end{tikzpicture} and solves the problem :-) –  petermeissner Mar 6 '13 at 15:19
    
Great! Do you mind we close it as too localized if you problem is solved then? –  percusse Mar 6 '13 at 15:21
    
@percusse It might help some others but I don't mind if it gets closed as too localized - in fact the question is solved. –  petermeissner Mar 6 '13 at 15:38
2  
No I mean you can answer your question too. Don't get me wrong. I was too fast to tell you that we kind of try to keep the unanswered list short and a quick option fixes are a little out of fashion here :) So please write an answer and excuse my manners :) –  percusse Mar 6 '13 at 15:47

1 Answer 1

up vote 8 down vote accepted

Making the example work:

The answer is to use a special coordinate system called "axis cs" (see the comment given by percusse above)

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}
\thispagestyle{empty}

\begin{tikzpicture}[domain=-10:10]
    \begin{axis}[axis lines=none]
        \addplot {exp(x)/(1+exp(x))};
        \draw[thick] (axis cs: -10,0) rectangle (axis cs: 10,1);        
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Concerning the sub-question "why is there a difference and which?"

First, let me stress: yes, there is a significant difference in the coordinate systems. And: yes, that is fully intentional; in fact: it is dictated by the different use-cases. Typically, a plot is about data visualization: it attempts to map data coordinates into visualization coordinates in a way which "makes good use of the available space". This requires rescaling of the data. Rescaling, in turn, means to use a different coordinate system. And pgfplots tries hard to hide all that complexity: with pgfplots, you simply provide the data and the result size (as width or height) and pgfplots does the rest.

Occasionally, one wants to combine both to some extend. Your use-case appears to be the desire to annotate the visualization, i.e. to draw some custom line.

Solutions are to use extra y tick combined with grid lines or to simply draw a line. If you draw a line, you place the drawing instruction inside of the axis and use (axis cs:-10,0) rectangle (axis cs:10,1) to identify the points inside of the "axis coordinate system".

Note that there is also a different use-case which is NOT data visualization. Occasionally, one wants to draw a graphical element, and the fact that its corners are identified by some coordinates has nothing to do with "visualize this data". Such applications imply to use the very same coordinate system for any TikZ instruction and for any pgfplots instruction. This is possible and is subject of a section of the pgfplots manual (called "TikZ Interoperability"). From my point of view, this is a relatively rare use-case.

The typical use-case is to enrich a data visualization by means of some drawing elements, i.e. the first one. Pgfplots supports this by means of the additional coordinate systems (there are a couple of further coordinate systems). It also plugs into the creation of circles/ellipses in order to express them by means of axis units.

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.