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

4
  • It is always best to compose a MWE that illustrates the problem including the \documentclass so that those trying to help don't have to recreate it. Commented Oct 9, 2011 at 14:24
  • Does this question help: pgfplot to occupy full linewidth and aligned y-axis across tikzpicture Commented Oct 9, 2011 at 14:28
  • @PeterGrill I also came across that question, I don't think it helps here
    – barbaz
    Commented Oct 9, 2011 at 14:35
  • @barbaz If Peter's answer is what you want can you upvote it and accept it please.
    – yannisl
    Commented Oct 9, 2011 at 14:57

2 Answers 2

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}

}

5

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .