# getting the same height for two side by side pgfplot figures

I display two side by side pgfplots which dont have the same xmax and ymax. In a first case with (xmin=-1,xmax=3,ymin=-1,ymax=11) the two plots have the same height (see EXAMPLE 1). In a second case with (xmin=-1,xmax=12,ymin=-1,ymax=7) the two plots do not have exactly the same height. But I would like them to have the same height (see EXAMPLE 2). Maybe one has to do something specific when ymax-ymin is less than xmax-xmin ?

EXAMPLE 1 (with the same height)

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{pifont}

\begin{document}

\def\argument#1{\mathtt{#1}}
\def\constraint#1{\textsc{#1}}

\begin{tikzpicture}[scale=0.7]
\begin{scope}[xshift=0cm,yshift=0cm]
\begin{axis}[axis equal image=true,xlabel={nb\_bump\_on\_decreasing\_sequence},
ylabel={nb\_decreasing},
title style={yshift=1.2ex},
title={$\argument{sv}=12$},
xtick ={0,2,...,2},
ytick ={0,2,...,10},
minor tick num=1,
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
xmin=-1,xmax=4,
ymin=-1,ymax=12,
extra description/.code={
\node (description1) at (c1) {\large\ding{172}};
\node (description2) at (c2) {\large\ding{173}};
\node (description3) at (c3) {\large\ding{174}};
\node (description4) at (c4) {\large\ding{175}};
}
]
\draw[fill=pink,opacity=0.1] (axis cs:-1.5,-1) -- (axis cs:5.0,12) -- (axis cs:5.0,12) --
(axis cs:5.0,-1) -- cycle;
\draw[fill=pink,opacity=0.1] (axis cs:-1,4) -- (axis cs:4,4) -- (axis cs:4,-1) --
(axis cs:-1,-1) -- cycle;
\draw[fill=cyan,opacity=0.2] (axis cs:-1,12.0) -- (axis cs:4,7.0) -- (axis cs:4,12) -- (axis cs:-1,12) -- cycle;
\coordinate (c1) at (axis cs:4.6,10.0);
\coordinate (c2) at (axis cs:4.6,4);
\coordinate (c3) at (axis cs:4.6,7.0);
\coordinate (c4) at (axis cs:4.6,3);
\end{axis}
\end{scope}
\begin{scope}[xshift=10cm,yshift=0cm]
\begin{axis}[axis equal image=true,xlabel={nb\_bump\_on\_decreasing\_sequence},
ylabel={nb\_decreasing},
title style={yshift=1.2ex},
title={$\argument{sv}=11$},
xtick ={0,2,...,3},
ytick ={0,2,...,11},
minor tick num=1,
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
xmin=-1,xmax=3,
ymin=-1,ymax=11,
extra description/.code={
\node (description1) at (c1) {\large\ding{172}};
\node (description2) at (c2) {\large\ding{173}};
\node (description3) at (c3) {\large\ding{174}};
\node (description4) at (c4) {\large\ding{175}};
}
]
\draw[fill=pink,opacity=0.1] (axis cs:-1.5,-1) -- (axis cs:4.5,11) -- (axis cs:4.5,11) --
(axis cs:4.5,-1) -- cycle;
\draw[fill=pink,opacity=0.1] (axis cs:-1,4) -- (axis cs:3,4) -- (axis cs:3,-1) --
(axis cs:-1,-1) -- cycle;
\draw[fill=cyan,opacity=0.2] (axis cs:-1,11.0) -- (axis cs:3,7.0) -- (axis cs:3,11) --
(axis cs:-1,11) -- cycle;
\coordinate (c1) at (axis cs:3.55,8.0);
\coordinate (c2) at (axis cs:3.55,4);
\coordinate (c3) at (axis cs:3.55,7.0);
\coordinate (c4) at (axis cs:3.55,3);
\end{axis}
\end{scope}
\end{tikzpicture}

\end{document}


EXAMPLE 2 (where the heights differ)

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{pifont}

\begin{document}

\def\argument#1{\mathtt{#1}}
\def\constraint#1{\textsc{#1}}

\begin{tikzpicture}[scale=0.7]
\begin{scope}[xshift=0cm,yshift=0cm]
\begin{axis}[axis equal image=true,xlabel={nb\_decreasing},
title style={yshift=1.2ex},
title={$\argument{sv}=12$},
xtick ={0,2,...,10},
ytick ={0,2,...,7},
minor tick num=1,
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
xmin=-1,xmax=12,
ymin=-1,ymax=7,
extra description/.code={
\node (description1) at (c1) {\large\ding{172}};
}
]
coordinates {(0,0)(0,1)(0,2)(0,3)(0,4)(0,5)(0,6)(1,0)(2,0)(3,0)(4,0)(5,0)(6,0)(7,0)(8,0)(9,0)(10,0)(11,0)};
coordinates {(1,1)(1,2)(1,3)(1,4)(1,5)(1,6)(2,1)(2,2)(2,3)(2,4)(2,5)(2,6)(3,1)(3,2)(3,3)(3,4)(3,5)(3,6)(4,1)(4,2)(4,3)(4,4)(4,5)(4,6)(5,1)(5,2)(5,3)(5,4)(5,5)(5,6)(6,1)(6,2)(6,3)(6,4)(6,5)(7,1)(7,2)(7,3)(7,4)(8,1)(8,2)(8,3)(9,1)(9,2)(10,1)};
coordinates {(0,0)(11,0)(5,6)(0,6)(0,0)(0,0)};
\draw[fill=cyan,opacity=0.2] (axis cs:-1,12.0) -- (axis cs:12,-1.0) -- (axis cs:12,12) --
(axis cs:-1,12) -- cycle;
\coordinate (c1) at (axis cs:12.6,-1.0);
\end{axis}
\end{scope}
\begin{scope}[xshift=10cm,yshift=0cm]
\begin{axis}[axis equal image=true,xlabel={nb\_decreasing},
title style={yshift=1.2ex},
title={$\argument{sv}=11$},
xtick ={0,2,...,11},
ytick ={0,2,...,4},
minor tick num=1,
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
xmin=-1,xmax=11,
ymin=-1,ymax=6,
extra description/.code={
\node (description1) at (c1) {\large\ding{172}};
}
]
\draw[fill=cyan,opacity=0.2] (axis cs:-1,11.0) -- (axis cs:11,-1.0) -- (axis cs:11,11) --
(axis cs:-1,11) -- cycle;
\coordinate (c1) at (axis cs:11.55,-1.0);
\end{axis}
\end{scope}
\end{tikzpicture}

\end{document}


• It's related to axis equal image=true, with that set to false they're the same height. – Torbjørn T. May 26 '18 at 7:28
• When I use axis equal image=false, it makes things worse, the two heights are more "unbalanced" – Nicolas Beldiceanu May 26 '18 at 7:49
• Not if you set that for both axis environments, then they're the same. – Torbjørn T. May 26 '18 at 8:03
• Shouldn't sv11 be shorter in both directions? For the sake of comparability, it would be better to have equal lengths of units in both plots, with differing plot height and width. Or you can expand sv11 plot to include one more unit to both y and x axes. This way, your plots will have the same dimension AND the same unit lengths. Much better comparability imho. – thymaro May 26 '18 at 10:23
• Unrelated: What do the diagrams represent? – Dr. Manuel Kuehner May 26 '18 at 13:13

I think perhaps what you're seeing is related to the documented behaviour for axis equal image. With that setting, pgfplots will happily rescale an axis so it no longer conforms to the height/width setting. The axis equal setting on the other hand, will respect the height/width setting, by adjusting the axis limits.

Depending on your requirements, either not using axis equal image, or using axis equal instead, might be a solution, and in addition set both width and height explicitly. (I used width=6cm, height=4.5cm below.)

Not related to your question, but you might be interested in the groupplots library, demonstrated below.

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.16}

\usepackage{pifont}
\newcommand\argument[1]{\mathtt{#1}}
\newcommand\constraint[1]{\textsc{#1}}

\begin{document}

Without \texttt{axis equal image}:

\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 1,
horizontal sep=1.5cm,
},
% set common options here
footnotesize, % reduces font size of diagram
xmin=-1,
ymin=-1,
width=6cm,
height=4.5cm,
xlabel={nb\_decreasing},
title style={yshift=1.2ex},
minor tick num=1,
xtick ={0,2,...,10},
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
extra description/.code={
\node (description1) at (c1) {\large\ding{172}};
}
]

\nextgroupplot[
title={$\argument{sv}=12$},
ytick ={0,2,...,7},
xmax=12,
ymax=7,
]
coordinates {(0,0)(0,1)(0,2)(0,3)(0,4)(0,5)(0,6)(1,0)(2,0)(3,0)(4,0)(5,0)(6,0)(7,0)(8,0)(9,0)(10,0)(11,0)};
coordinates {(1,1)(1,2)(1,3)(1,4)(1,5)(1,6)(2,1)(2,2)(2,3)(2,4)(2,5)(2,6)(3,1)(3,2)(3,3)(3,4)(3,5)(3,6)(4,1)(4,2)(4,3)(4,4)(4,5)(4,6)(5,1)(5,2)(5,3)(5,4)(5,5)(5,6)(6,1)(6,2)(6,3)(6,4)(6,5)(7,1)(7,2)(7,3)(7,4)(8,1)(8,2)(8,3)(9,1)(9,2)(10,1)};
coordinates {(0,0)(11,0)(5,6)(0,6)(0,0)(0,0)};
\draw[fill=cyan,opacity=0.2] (axis cs:-1,12.0) -- (axis cs:12,-1.0) -- (axis cs:12,12) --
(axis cs:-1,12) -- cycle;
\coordinate (c1) at (axis cs:12.6,-1.0);

\nextgroupplot[
title={$\argument{sv}=11$},
ytick ={0,2,...,4},
xmax=11,
ymax=6,
]
\draw[fill=cyan,opacity=0.2] (axis cs:-1,11.0) -- (axis cs:11,-1.0) -- (axis cs:11,11) --
(axis cs:-1,11) -- cycle;
\coordinate (c1) at (axis cs:11.55,-1.0);
\end{groupplot}

\end{tikzpicture}

With \texttt{axis equal} instead of \texttt{axis equal image}:

\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 1,
horizontal sep=1.5cm,
},
% set common options here
axis equal,
footnotesize, % reduces font size of diagram
xmin=-1,
ymin=-1,
width=6cm,
height=4.5cm,
xlabel={nb\_decreasing},
title style={yshift=1.2ex},
minor tick num=1,
xtick ={0,2,...,10},
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
extra description/.code={
\node (description1) at (c1) {\large\ding{172}};
}
]

\nextgroupplot[
title={$\argument{sv}=12$},
ytick ={0,2,...,7},
xmax=12,
ymax=7,
]
coordinates {(0,0)(0,1)(0,2)(0,3)(0,4)(0,5)(0,6)(1,0)(2,0)(3,0)(4,0)(5,0)(6,0)(7,0)(8,0)(9,0)(10,0)(11,0)};
coordinates {(1,1)(1,2)(1,3)(1,4)(1,5)(1,6)(2,1)(2,2)(2,3)(2,4)(2,5)(2,6)(3,1)(3,2)(3,3)(3,4)(3,5)(3,6)(4,1)(4,2)(4,3)(4,4)(4,5)(4,6)(5,1)(5,2)(5,3)(5,4)(5,5)(5,6)(6,1)(6,2)(6,3)(6,4)(6,5)(7,1)(7,2)(7,3)(7,4)(8,1)(8,2)(8,3)(9,1)(9,2)(10,1)};
coordinates {(0,0)(11,0)(5,6)(0,6)(0,0)(0,0)};
\draw[fill=cyan,opacity=0.2] (axis cs:-1,12.0) -- (axis cs:12,-1.0) -- (axis cs:12,12) --
(axis cs:-1,12) -- cycle;
\coordinate (c1) at (axis cs:12.6,-1.0);

\nextgroupplot[
title={$\argument{sv}=11$},
ytick ={0,2,...,4},
xmax=11,
ymax=6,
]
\draw[fill=cyan,opacity=0.2] (axis cs:-1,11.0) -- (axis cs:11,-1.0) -- (axis cs:11,11) --
(axis cs:-1,11) -- cycle;

• @NicolasBeldiceanu By the way, if that circled 1 is supposed to be next to the bottom right corner of the axis, you can use \node [right,inner sep=1pt] (description1) at (current axis.south east) {\large\ding{172}};, which means you don't need to define the c1 coordinate. – Torbjørn T. May 26 '18 at 9:39
• @NicolasBeldiceanu I see. Just thinking out loud here: add scale only axis to the axis options, and choose some suitable maximum size (e.g. maxsize=5cm). If yrange > xrange, set the height to maxsize, and the width to maxsize * xrange/yrange. Similarly, if yrange < xrange, set the width to maxsize, and the height to maxsize * yrange/xrange. Of course if yrange=xrange, set both width and height to maxsize. – Torbjørn T. May 26 '18 at 10:09