# pgfplots: How to get values of width and height of axis rectangle?

When using groupplots, it is useful to not to use the scale only axis option so the overall width of the plots can be made to match the text width, for example. The drawback of this option (if it can be called that) is that the width and height of the axis rectangle is not known.

However, for reasons that are not important here, I need to know the width and height of the axis rectangles in a groupplot. How can I get pgfplots to print these values?

MWE:

\documentclass[10pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\begin{document}
\begin{tikzpicture}

\begin{groupplot}[
width=6.0cm,height=6.0cm,
enlargelimits=false,
group style={group size=2 by 1,xlabels at=edge bottom},
xlabel=$x$]

\nextgroupplot[xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,ylabel=$y$]

\nextgroupplot[xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,ylabel=$y$]
\end{groupplot}

\end{tikzpicture}
\end{document}

This is conceptually similar to John Kormylo's answer (+1) but differs in two ways. First, it addresses the problem also for group plots and second it does not introduce new length but declares pgf functions nodewidth and nodeheight. I just took my last group plot answer as a basis. In order to measure group plots, you can either use alias or the group name keys. For the latter, if you use, say, group name=my fancy plots, the group plots will have the names my fancy plots c<x>r<y>,where x and y indicate the column and row, see the pgfplots manual on p. 457. This code

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{pgfplots.groupplots}
\usetikzlibrary{arrows.meta}
\makeatletter
\pgfmathdeclarefunction{nodewidth}{1}{\begingroup%
\pgfpointdiff{\pgfpointanchor{#1}{south west}}{%
\pgfpointanchor{#1}{north east}}%
\edef\pgfmathresult{\the\pgf@x}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\pgfmathdeclarefunction{nodeheight}{1}{\begingroup%
\pgfpointdiff{\pgfpointanchor{#1}{south west}}{%
\pgfpointanchor{#1}{north east}}%
\edef\pgfmathresult{\the\pgf@y}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\makeatother
\begin{document}
\begin{tikzpicture}[scale=1]

\begin{groupplot}[
group style={
group name=my fancy plots,% <
group size=2 by 1,
horizontal sep=0pt
},
ymin=0, ymax=1.5
]
\nextgroupplot[ymin=0, ymax=1.5,height=6cm,
axis lines=middle, xtick={-1,...,1},
xticklabels={-1,...,1},
enlarge x limits=0.5,
enlarge y limits=0.5,
extra x tick style={
xticklabel style={yshift=0.5ex, anchor=south}},
xmin=-1.5,xmax=1.5, ytick={\empty}, yticklabels={},
axis on top,
axis line style={-Latex[round]},
every inner x axis line/.append style={-},
]
{(-1,0) (0,1) (1,0)};
\node[anchor=east] at (axis cs:0,1.5) {$x[n]$};

\nextgroupplot[xmin=999, xmax=1001,
axis lines=middle,height=6cm,width=5cm,
hide y axis,
ymin=-0.5, ymax=1.5,
xtick=1000,
xticklabels=1000,
axis x discontinuity=crunch,alias=gp2
]
{(999,0) (1000,1) (1001,0)};
\node[anchor=south] at (axis cs:1000,1) {$1$};
\node[anchor=north] at (axis cs:1001.5,0) {$n$};
\end{groupplot}
\foreach \X in {1,2}
{\pgfmathsetmacro{\myx}{nodewidth("my fancy plots c\X r1")}
\pgfmathsetmacro{\myy}{nodeheight("my fancy plots c\X r1")}
\typeout{plot \X\space has width \myx\space and height \myy}}
\pgfmathsetmacro{\myx}{nodewidth("gp2")}
\pgfmathsetmacro{\myy}{nodeheight("gp2")}
\typeout{plot 2 has width \myx\space and height \myy}
\end{tikzpicture}%
\end{document}

will tell you

plot 1 has width 152.92969pt and height 125.71564pt
plot 2 has width 97.26349pt and height 125.71411pt
plot 2 has width 97.26349pt and height 125.71411pt

where the last output shows that alias also works.

\documentclass[10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{groupplots}
\makeatletter
\pgfmathdeclarefunction{nodewidth}{1}{\begingroup%
\pgfpointdiff{\pgfpointanchor{#1}{south west}}{%
\pgfpointanchor{#1}{north east}}%
\edef\pgfmathresult{\the\pgf@x}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\pgfmathdeclarefunction{nodeheight}{1}{\begingroup%
\pgfpointdiff{\pgfpointanchor{#1}{south west}}{%
\pgfpointanchor{#1}{north east}}%
\edef\pgfmathresult{\the\pgf@y}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\makeatother
\begin{document}
\begin{tikzpicture}

\begin{groupplot}[
width=6.0cm,height=6.0cm,
enlargelimits=false,
group style={group size=2 by 1,xlabels at=edge bottom,
},
xlabel=$x$]

\nextgroupplot[xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,ylabel=$y$]

\nextgroupplot[xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,ylabel=$y$]
\end{groupplot}
\foreach \X in {1,2}
{\pgfmathsetmacro{\myx}{nodewidth("my plots c\X r1")}
\pgfmathsetmacro{\myy}{nodeheight("my plots c\X r1")}
\typeout{plot \X\space has width \myx\space and height \myy}}
\end{tikzpicture}
\end{document}

one gets

plot 1 has width 125.71564pt and height 125.71564pt
plot 2 has width 125.71564pt and height 125.71564pt
• Thanks, this does what I need. I was just going to ask about some of the extra lines in the foreach loop but your edit has taken care of it... – user1362373 Feb 16 at 19:46

Since these are used inside the tikzpicture, one need not save them globally.

While the origin is located at (A0), I wouldn't rely on that.

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\begin{document}
\begin{tikzpicture}

\begin{groupplot}[
width=6.0cm,height=6.0cm,
enlargelimits=false,
group style={group size=2 by 1,xlabels at=edge bottom},
xlabel=$x$]

\nextgroupplot[xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,ylabel=$y$]
\coordinate (A0) at (rel axis cs: 0,0);
\coordinate (A1) at (rel axis cs: 1,1);

\nextgroupplot[xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,ylabel=$y$]
\coordinate (B0) at (rel axis cs: 0,0);
\coordinate (B1) at (rel axis cs: 1,1);
\end{groupplot}

\path ($(A1)-(A0)$);
\pgfgetlastxy{\Awidth}{\Aheight}
\node[below right] at (current bounding box.south west) {First width \Awidth};
\node[below right] at (current bounding box.south west) {First height \Aheight};

\path ($(B1)-(B0)$);
\pgfgetlastxy{\Bwidth}{\Bheight}
\node[below right] at (current bounding box.south west) {Second width \Bwidth};
\node[below right] at (current bounding box.south west) {Second height \Bheight};
\end{tikzpicture}
\end{document}

• Thanks for the quick reply. However, I can't get this to work with groupplots- no matter where I put the name=border, I get the message Package pgf Error: No shape named border' is known. – user1362373 Feb 16 at 18:40
• Ah, no axis environment used with groupplot. Sorry abut that. – John Kormylo Feb 16 at 19:35
• I edited my question to contain an MWE. (I don't see what the (relationship axis cs: 1,1) would change as far as the error is concerned.) – user1362373 Feb 16 at 19:36
• It would be good to get both. The answer of @Schrödinger's cat takes care of it. – user1362373 Feb 16 at 19:51