10

I have two axis environments both containing different plots. I do however want to maintain comparability between both, i.e. I want both plots to consume the same space on the paper for a coordinate step on the y-axis. Setting both plots to the same height doesn't do the trick, as both have different ymax values, thus the ratios of both plots would be different.

In this example

\documentclass[border=3mm]{standalone}

\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
        \begin{axis}
                \addplot coordinates {
                (0,0)
                (1,1)
                };
        \end{axis}
\end{tikzpicture}

\begin{tikzpicture} 
        \begin{axis}
                \addplot coordinates {
                (0,0)
                (1,1)
                };
                \addplot coordinates {
                (0,0)
                (1,2)
                };
        \end{axis}
\end{tikzpicture}
\end{document}

}

... I would want the second axis box to be automatically double as high as the first one, such that the two identical plots also are identically scaled across the two axis environments. The actual outcome does however look like this:

enter image description here

10

You can save the y unit vector length of the first plot into a macro by using

\pgfplotsextra{
    \global\edef\yunit{\pgfplotsunitylength}
}

which can then be used to set the y unit vector in the second plot by setting y=\yunit. This will only work correctly if you use disabledatascaling, though, otherwise the unit vector will not be reported correctly.

\documentclass{article}

\usepackage{pgfplots}
\begin{document}

\pgfplotsset{
    scale only axis, % To make sure the plot *areas* are the same width
    width=5cm,
    enlarge y limits={abs=0.2}, % To make sure the axes are extended by the same absolute amount
    disabledatascaling
}

\begin{tikzpicture}
        \begin{axis}
                \addplot coordinates {
                (0,0)
                (1,2)
                };
        \pgfplotsextra{
            \global\edef\yunit{\pgfplotsunitylength}
        }
        \end{axis}
\end{tikzpicture}
\begin{tikzpicture} 
        \begin{axis}[y=\yunit]
                \addplot coordinates {
                (0,0)
                (1,2)
                };
                \addplot coordinates {
                (0,0)
                (1,3)
                };
        \end{axis}
\end{tikzpicture}
\end{document}

}

4

Not quite automatic, but you could specify the height, or specify the y-unit vector length.

Perhaps you use can do some automation to determine the scale factor via the values of ymin ymax. These are accessible via \pgfkeysvalueof{/pgfplots/xmin}, but as per How to access \xmin, \xmax, \ymin, \ymax from within PGFplots axis environment you can only access them if they have been set explicitly in the axis options.

\documentclass[border=3mm]{standalone}

\usepackage{pgfplots}
\begin{document}

\pgfplotsset{yticklabel style={text width=3em,align=right}}
\begin{tikzpicture}
        \begin{axis}[height=6.0cm,width=10cm,scale only axis]
                \addplot coordinates {(0,0) (1,1)};
        \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
        \begin{axis}[height=12.0cm,width=10cm,scale only axis]
                \addplot coordinates {(0,0) (1,1)};
                \addplot coordinates {(0,0) (1,2)};
        \end{axis}
\end{tikzpicture}
\end{document}

Also not automatic, but you could specify the length of the unit vectors by [y={<dimension>}].

\documentclass[border=3mm]{standalone}

\usepackage{pgfplots}
\begin{document}

\pgfplotsset{yticklabel style={text width=3em,align=right}}
\begin{tikzpicture}
        \begin{axis}[y=4cm]
                \addplot coordinates {(0,0) (1,1)};
        \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
        \begin{axis}[y=4cm]
                \addplot coordinates {(0,0) (1,1)};
                \addplot coordinates {(0,0) (1,2)};
        \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Yes, thats what I'm doing at the moment. And thats what I want a replacement for :o) – barbaz Oct 9 '11 at 14:52
  • See updated re possible automation, but that require explicitly specifying the ymin, yamx values, or alternatively specifying the unit vector length. – Peter Grill Oct 9 '11 at 15:09

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.