4

I was trying to define an axis environment within a node. Upon adding multiple addplots I observed a shift in the axis position, shifting further for more addplots. Below is an example to recreate the problem:

\documentclass{article}

\usepackage{tikz,pgfplots}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
%
\node at (0.0,0.0) (A) {x};%
\node at (0.0,2.0) (B) {x};%
\node at (0.0,4.0) (C) {x};%
%
\node at (A) {%
\begin{axis}[domain=1:2,width=40mm,height=30mm]%
\addplot {sin(deg(x))};%
\end{axis}%
};%
%
\node at (B) {%
\begin{axis}[domain=1:2,width=40mm,height=30mm]%
\addplot {sin(deg(x))};%
\addplot {sin(deg(x))};%
\end{axis}%
};%
%
\node at (C) {%
\begin{axis}[domain=1:2,width=40mm,height=30mm]%
\addplot {sin(deg(x))};%
\addplot {sin(deg(x))};%
\addplot {sin(deg(x))};%
\end{axis}%
};%
%
\end{tikzpicture}
\end{figure}

\end{document}

A similar problem was discussed here: Why does \addplot shift my graph of a \pgfmathdeclarefunction function to the right?, which is the reason why I added the % signs at the end of each line but this does not solve the shifting issue here.

  • A workaround which I just came across would be to remove the nodes, but use \begin{axis}[...,at=(A)] instead. This does seem to align all three plots. – Erik May 13 '15 at 8:33
  • Just curiosity, why do you want an axis inside a node? If it's just aligning them you can align them with tabular, a TiKZ matrix or a groupplot. All of them explained in "4.19 Alignment options" – Ignasi May 13 '15 at 8:54
  • I'm surprised it works at all. Generally you have to use \tikz{...} inside the node to be able to access tikz commands. – John Kormylo May 14 '15 at 4:22
  • Ignasi, I use this to position a graph on top of another graph. Some form of picture-in-picture if you will. The example was only to show the shift in position and alignment of the graphs could for the example indeed be achieved by using a tabular. – Erik May 18 '15 at 9:22
5

It is because you are doing something that shouldn't be done. What I mean by that is just because you can do it does not imply you should do it. The problem here is a common one which is the \nullfont disabling and making spaces matter.

I've tried to describe it here very briefly Setting a length with a key to a TikZ node that inside the node the regular space and text gobbling behavior of TikZ is turned off to be able to let TeX typeset the text. But you are inserting the code within the node without this convenience.

For example, if I add \nullfont to the first node I get the expected result

enter image description here

Hence, don't use nodes' text area as a placeholder for complicated objects. In your case, the proper way to do this is to scope every axis and move around if they are independent via \begin{scope}[shift={(3cm,5cm)}] \end{scope} within the same TikZ picture or use the name option of the axes such as

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{width=3cm}
    \begin{axis}[name=a]\end{axis}
    \begin{axis}[anchor=north east, at = (a.south west)]\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

or many other possible positioning and anchoring options.

0

I would report this to pgfplots It is really strange how spurious spaces are handled here. For the time being, you may write:

% arara: pdflatex

\documentclass{article}
\usepackage{pgfplots}

\begin{document}    
\begin{figure}
    \centering
    \begin{tikzpicture}
        \coordinate (A) at (0.0,0.0);
        \coordinate (B) at (0.0,2.0);
        \coordinate (C) at (0.0,4.0);
        \node at (A) {
            \begin{axis}[domain=1:2,width=40mm,height=30mm]
            \addplot {sin(deg(x))};
            \end{axis}
        };
        \node at (B) {
            \begin{axis}[domain=1:2,width=40mm,height=30mm]
            \addplot {sin(deg(x))};%
            \addplot {sin(deg(x))};%
            \end{axis}
        };
        \node at (C) {
            \begin{axis}[domain=1:2,width=40mm,height=30mm]%
            \addplot {sin(deg(x))};%
            \addplot {sin(deg(x))};%
            \addplot {sin(deg(x))};%
            \end{axis}
        };
    \end{tikzpicture}
\end{figure}    
\end{document}

I have just added % signs where they are really needed.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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