pgfplots:如何获取轴矩形的宽度和高度值?

pgfplots:如何获取轴矩形的宽度和高度值?

使用时groupplots不是例如,使用该scale only axis选项可以使图表的整体宽度与文本宽度相匹配。此选项的缺点(如果可以这样称呼的话)是轴矩形的宽度和高度是未知的。

但是,出于这里不重要的原因,我需要知道轴矩形的宽度和高度groupplot。我怎样才能pgfplots打印这些值?

梅威瑟:

\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$]
  \addplot coordinates {(0.1,0.1) (0.9,0.9)};

  \nextgroupplot[xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,ylabel=$y$]
  \addplot coordinates {(0.1,0.9) (0.9,0.1)};
\end{groupplot}

\end{tikzpicture}
\end{document}

答案1

这在概念上类似于John Kormylo 的回答(+1) 但有两点不同。首先,它也解决了组图的问题;其次,它没有引入新的长度,但声明了 pgf 函数nodewidthnodeheight。我只是以我上一个组图答案为基础。为了测量组图,您可以使用aliasgroup name键。对于后者,如果您使用,例如,group name=my fancy plots组图将具有名称my fancy plots c<x>r<y>,其中xy指示列和行,请参阅第 457 页的 pgfplots 手册。此代码

\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={-},
      ]
    \addplot+[ycomb,black,very thick] plot coordinates
      {(-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
 ]
 \addplot+[ycomb,black,very thick] plot coordinates
 {(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}

会告诉你

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

最后的输出表明这alias也有效。

或者应用到你的情节:

\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,
  group name=my plots,%<- added
  },
  xlabel=$x$]

  \nextgroupplot[xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,ylabel=$y$]
  \addplot coordinates {(0.1,0.1) (0.9,0.9)};

  \nextgroupplot[xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,ylabel=$y$]
  \addplot coordinates {(0.1,0.9) (0.9,0.1)};
\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}

一个人

plot 1 has width 125.71564pt and height 125.71564pt
plot 2 has width 125.71564pt and height 125.71564pt

答案2

由于这些是在 tikzpicture 内部使用的,因此不需要全局保存它们。

虽然原点位于(A0),但我不会依赖它。

\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$]
  \addplot coordinates {(0.1,0.1) (0.9,0.9)};
  \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$]
  \addplot coordinates {(0.1,0.9) (0.9,0.1)};
  \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}

演示

相关内容