pgfplots - 如何更改所有后续组图的选项(例如 xlabel)?

pgfplots - 如何更改所有后续组图的选项(例如 xlabel)?

如果在 groupplots 选项中定义了xlabelylabel,则这些值将用于所有后续绘图。其他选项相同。

是否可以xlabel从一点开始更改所有后续组图的,而不必为每个组图重复该选项?当想要在 -环境中定义每行的ymin和的值时,这特别方便。ymaxgroupplots

图片

在此处输入图片描述

平均能量损失

\documentclass[
a4paper
]{scrartcl}

\usepackage{
lmodern,
tikz,
pgfplots,
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepgfplotslibrary{groupplots}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\footnotesize]
\begin{groupplot}[
group style={
group size=2 by 2,
horizontal sep=0.2cm,
vertical sep=1.5cm,
ylabels at=edge left,
yticklabels at=edge left,
},
%
ymin=300,
ymax=1300,
%
xlabel={The label for the x-axis},
ylabel={Some y-values},
%
scale ticks above exponent={2},
]
\nextgroupplot
\addplot+ coordinates{(2200,1200) (3100,500)};
\nextgroupplot
\addplot+ coordinates{(2300,1200) (4000,400)};
\nextgroupplot[xlabel={Now I define another xlabel2}]
\addplot+ coordinates{(2400,1200) (4500,400)};
\nextgroupplot[xlabel={And here I have to repeat it otherwise the label for the first plots gets put here}, xlabel style={text width=5cm}]
\addplot+ coordinates{(2500,1200) (4600,400)};
\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}

答案1

如果您不介意groupplots稍微增强一下库的一个内部宏,您可以定义一种新sticky options样式,可以在可选参数中使用该样式来\nextgroupplot[...]设置在环境的剩余时间内保持活动状态的选项groupplots(或直到您sticky options再次调用)。

将以下代码块放入您的序言中将使其sticky options可用。

\makeatletter
\pgfplotsset{
    sticky options/.store in=\pgfplots@group@stickyoptions,
    sticky options={}
}

\def\pgfplots@group@nextplot[#1]{%
    % Check if it legal to produce another plot
    \ifnum\pgfplots@group@current@plot=\pgfplots@group@totalplots\relax
      \pgfplotswarning{groupplots/too many plots}{\the\pgfplots@group@current@plot}{\pgfplots@group@totalplots}\pgfeov
    \else
      % 
      % If there has been other plots before then end the plot
      % 
      \ifnum0<\pgfplots@group@current@plot\relax
        % DEBUG: \message{DEBUG: Ending plot}
        \endpgfplots@environment@opt
      \fi

      % Use a group so we can discard all the options that aren't sticky
      \begingroup
      % Execute the keys
      \pgfqkeys{/pgfplots}{#1}
      % Smuggle the contents of the sticky options key out of the group
      \pgfmath@smuggleone\pgfplots@group@stickyoptions
      % End the group
      \endgroup
      % Prepare for next plot, increment plot, row and column
      \pgfplots@group@increment@numbers
      % DEBUG: \message{DEBUG: Cur. Plot: \the\pgfplots@group@current@plot. Current Row: \the\pgfplots@group@current@row. Current Column: \the\pgfplots@group@current@column.}    
      % Creation of the new axis environment
      \pgfplots@group@environment@create{#1, \pgfplots@group@stickyoptions}
    \fi
}
\makeatother

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\makeatletter
\pgfplotsset{
    sticky options/.store in=\pgfplots@group@stickyoptions,
    sticky options={}
}

\def\pgfplots@group@nextplot[#1]{%
    % Check if it legal to produce another plot
    \ifnum\pgfplots@group@current@plot=\pgfplots@group@totalplots\relax
      \pgfplotswarning{groupplots/too many plots}{\the\pgfplots@group@current@plot}{\pgfplots@group@totalplots}\pgfeov
    \else
      % 
      % If there has been other plots before then end the plot
      % 
      \ifnum0<\pgfplots@group@current@plot\relax
        % DEBUG: \message{DEBUG: Ending plot}
        \endpgfplots@environment@opt
      \fi

      % Use a group so we can discard all the options that aren't sticky
      \begingroup
      % Execute the keys
      \pgfqkeys{/pgfplots}{#1}
      % Smuggle the contents of the sticky options key out of the group
      \pgfmath@smuggleone\pgfplots@group@stickyoptions
      % End the group
      \endgroup
      % Prepare for next plot, increment plot, row and column
      \pgfplots@group@increment@numbers
      % DEBUG: \message{DEBUG: Cur. Plot: \the\pgfplots@group@current@plot. Current Row: \the\pgfplots@group@current@row. Current Column: \the\pgfplots@group@current@column.}    
      % Creation of the new axis environment
      \pgfplots@group@environment@create{#1, \pgfplots@group@stickyoptions}
    \fi
}
\makeatother

\begin{document}
\begin{tikzpicture}[font=\footnotesize]
\begin{groupplot}[
group style={
group size=2 by 2,
horizontal sep=0.2cm,
vertical sep=1.5cm,
ylabels at=edge left,
yticklabels at=edge left,
},
%
ymin=300,
ymax=1300,
%
xlabel={The label for the x-axis},
ylabel={Some y-values},
%
scale ticks above exponent={2},
]
\nextgroupplot
\addplot+ coordinates{(2200,1200) (3100,500)};
\nextgroupplot
\addplot+ coordinates{(2300,1200) (4000,400)};
\nextgroupplot[sticky options={xlabel={Now I define another xlabel2}, ymin=0}]
\addplot+ coordinates{(2400,1200) (4500,400)};
\nextgroupplot
\addplot+ coordinates{(2500,1200) (4600,400)};
\end{groupplot}
\end{tikzpicture}


\end{document}

答案2

如果将 x 标签保存在宏中这个小弯路是可以的,那么您可以执行以下操作:

\documentclass[
a4paper
]{scrartcl}

\usepackage{
lmodern,
tikz,
pgfplots,
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepgfplotslibrary{groupplots}

\newcommand\somexlabel{Label for $x$-axis}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\footnotesize]
\begin{groupplot}[
group style={
group size=2 by 2,
horizontal sep=0.2cm,
vertical sep=1.5cm,
ylabels at=edge left,
yticklabels at=edge left,
},
%
ymin=300,
ymax=1300,
%
xlabel={\somexlabel},
ylabel={Some y-values},
%
scale ticks above exponent={2},
width=6.5cm
]
\nextgroupplot
\addplot+ coordinates{(2200,1200) (3100,500)};
\nextgroupplot
\addplot+ coordinates{(2300,1200) (4000,400)};
\nextgroupplot
\xdef\somexlabel{New fancy label}
\addplot+ coordinates{(2400,1200) (4500,400)};
\nextgroupplot
\addplot+ coordinates{(2500,1200) (4600,400)};

\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}

相关内容