pgfplots - 如何在组图中仅打印一次 y 刻度标签?

pgfplots - 如何在组图中仅打印一次 y 刻度标签?

我想只打印 y 轴的刻度标签一次在 groupplot 中。这对于使用以下选项的情况非常有用:

ylabels at=edge left,
yticklabels at=edge left,

这可能吗?

图片

在此处输入图片描述

平均能量损失

\documentclass[
a4paper
]{scrartcl}

\usepackage{
amsmath,
tikz,
pgfplots,
}

\usepgfplotslibrary{
groupplots,
}

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


\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{groupplot}[
group style={
    group size=4 by 4, %4 column(s), 4 row(s)
    horizontal sep=0cm,
    vertical sep=1.5cm,
    ylabels at=edge left,
    yticklabels at=edge left,
},
xlabel={Bla},
ylabel={Process},
%
every x tick scale label/.append style={
    at={(0.95,0)},yshift=-1.5em,anchor=north,inner sep=0pt
},
every y tick scale label/.append style={
    at={(-0.05,0.99)},anchor=east,inner sep=0pt
},
scale ticks above exponent={2}
]
\nextgroupplot
\addplot[only marks] coordinates{(1,1200) (3,500)};
\nextgroupplot
\addplot[only marks] coordinates{(2,1200) (4,400)};
\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}

答案1

这似乎是groupplots库中的一个疏忽。修复此行为所需的更改并不复杂(只需一行代码),但它会影响一个非常长的内部宏。

要修复该库,请将以下代码放入您的序言中:

\makeatletter
%
% Determine the ticklabels of both x and y.
%
\def\pgfplots@group@determine@ticklabels{%
    \def\pgfplots@loc@TMPa{all}
    \def\pgfplots@loc@TMPb{top}
    \def\pgfplots@loc@TMPc{lower}
    \ifx\pgfplots@loc@TMPa\pgfplots@group@xticklabels%
      % Do nothing as all should have xticklabels
    \fi
    \gdef\pgfplots@glob@TMPa{}
    \ifx\pgfplots@loc@TMPb\pgfplots@group@xticklabels%
      \foreach \pgfplots@column in {1,...,\pgfplots@group@columns} {%
          \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
              \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r1/.append style={xticklabel pos=upper}}%
          \ifnum1<\pgfplots@group@rows\relax
            \foreach \pgfplots@row in {2,...,\pgfplots@group@rows} {%
                \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
                    \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r\pgfplots@row/.append style={/pgfplots/xticklabels={}}}%
            }%
          \fi%
      }%
    \fi
    \ifx\pgfplots@loc@TMPc\pgfplots@group@xticklabels%
      \foreach \pgfplots@column in {1,...,\pgfplots@group@columns} {%
          \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
              \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r\pgfplots@group@rows/.append style={xticklabel pos=lower}}%
          \ifnum1<\pgfplots@group@rows\relax
            \pgfplotsgroupplotsmath@decrease\pgfplots@group@rows
            \foreach \pgfplots@row in {1,...,\pgfmathresult} {%
                \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
                    \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r\pgfplots@row/.append style={/pgfplots/xticklabels={}}}%
            }%
          \fi%
      }%
    \fi%
    % 
    % Defining styles for the ytick's
    % 
    \def\pgfplots@loc@TMPb{left}
    \def\pgfplots@loc@TMPc{right}
    \ifx\pgfplots@loc@TMPa\pgfplots@group@yticklabels%
      % Do nothing as all should have yticklabels
    \fi
    \ifx\pgfplots@loc@TMPb\pgfplots@group@yticklabels%
      \foreach \pgfplots@row in {1,...,\pgfplots@group@rows} {%
          \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
              \pgfplots@glob@TMPa,/pgfplots/group/plot c1r\pgfplots@row/.append style={yticklabel pos=left}}%
          \ifnum1<\pgfplots@group@columns\relax
            \foreach \pgfplots@column in {2,...,\pgfplots@group@columns} {%
                \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
                    \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r\pgfplots@row/.append style={/pgfplots/yticklabels={}, /pgfplots/ytick scale label code/.code={}}}%
            }%
          \fi%
      }%
    \fi
    \ifx\pgfplots@loc@TMPc\pgfplots@group@yticklabels%
      \foreach \pgfplots@row in {1,...,\pgfplots@group@rows} {%
          \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
              \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@group@columns r\pgfplots@row/.append style={yticklabel pos=right}}%
          \ifnum1<\pgfplots@group@columns\relax
            \pgfplotsgroupplotsmath@decrease\pgfplots@group@columns
            \foreach \pgfplots@column in {1,...,\pgfmathresult} {%
                \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
                    \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r\pgfplots@row/.append style={/pgfplots/yticklabels={}, /pgfplots/ytick scale label code/.code={}}}%
            }%
          \fi%
      }%
    \fi%
    \expandafter\pgfkeys\expandafter{\pgfplots@glob@TMPa}
}
\makeatother

\documentclass{article}

\usepackage{pgfplots}

\usepgfplotslibrary{groupplots}

\makeatletter
%
% Determine the ticklabels of both x and y.
%
\def\pgfplots@group@determine@ticklabels{%
    \def\pgfplots@loc@TMPa{all}
    \def\pgfplots@loc@TMPb{top}
    \def\pgfplots@loc@TMPc{lower}
    \ifx\pgfplots@loc@TMPa\pgfplots@group@xticklabels%
      % Do nothing as all should have xticklabels
    \fi
    \gdef\pgfplots@glob@TMPa{}
    \ifx\pgfplots@loc@TMPb\pgfplots@group@xticklabels%
      \foreach \pgfplots@column in {1,...,\pgfplots@group@columns} {%
          \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
              \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r1/.append style={xticklabel pos=upper}}%
          \ifnum1<\pgfplots@group@rows\relax
            \foreach \pgfplots@row in {2,...,\pgfplots@group@rows} {%
                \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
                    \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r\pgfplots@row/.append style={/pgfplots/xticklabels={}}}%
            }%
          \fi%
      }%
    \fi
    \ifx\pgfplots@loc@TMPc\pgfplots@group@xticklabels%
      \foreach \pgfplots@column in {1,...,\pgfplots@group@columns} {%
          \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
              \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r\pgfplots@group@rows/.append style={xticklabel pos=lower}}%
          \ifnum1<\pgfplots@group@rows\relax
            \pgfplotsgroupplotsmath@decrease\pgfplots@group@rows
            \foreach \pgfplots@row in {1,...,\pgfmathresult} {%
                \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
                    \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r\pgfplots@row/.append style={/pgfplots/xticklabels={}}}%
            }%
          \fi%
      }%
    \fi%
    % 
    % Defining styles for the ytick's
    % 
    \def\pgfplots@loc@TMPb{left}
    \def\pgfplots@loc@TMPc{right}
    \ifx\pgfplots@loc@TMPa\pgfplots@group@yticklabels%
      % Do nothing as all should have yticklabels
    \fi
    \ifx\pgfplots@loc@TMPb\pgfplots@group@yticklabels%
      \foreach \pgfplots@row in {1,...,\pgfplots@group@rows} {%
          \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
              \pgfplots@glob@TMPa,/pgfplots/group/plot c1r\pgfplots@row/.append style={yticklabel pos=left}}%
          \ifnum1<\pgfplots@group@columns\relax
            \foreach \pgfplots@column in {2,...,\pgfplots@group@columns} {%
                \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
                    \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r\pgfplots@row/.append style={/pgfplots/yticklabels={}, /pgfplots/ytick scale label code/.code={}}}%
            }%
          \fi%
      }%
    \fi
    \ifx\pgfplots@loc@TMPc\pgfplots@group@yticklabels%
      \foreach \pgfplots@row in {1,...,\pgfplots@group@rows} {%
          \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
              \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@group@columns r\pgfplots@row/.append style={yticklabel pos=right}}%
          \ifnum1<\pgfplots@group@columns\relax
            \pgfplotsgroupplotsmath@decrease\pgfplots@group@columns
            \foreach \pgfplots@column in {1,...,\pgfmathresult} {%
                \expandafter\xdef\expandafter\pgfplots@glob@TMPa\expandafter{%
                    \pgfplots@glob@TMPa,/pgfplots/group/plot c\pgfplots@column r\pgfplots@row/.append style={/pgfplots/yticklabels={}, /pgfplots/ytick scale label code/.code={}}}%
            }%
          \fi%
      }%
    \fi%
    \expandafter\pgfkeys\expandafter{\pgfplots@glob@TMPa}
}
\makeatother

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{groupplot}[
group style={
    group size=2 by 1, %4 column(s), 4 row(s)
    horizontal sep=0cm,
    vertical sep=1.5cm,
    ylabels at=edge left,
    yticklabels at=edge left,
},
xlabel={Bla},
ylabel={Process},
%
every x tick scale label/.append style={
    at={(0.95,0)},yshift=-1.5em,anchor=north,inner sep=0pt
},
every y tick scale label/.append style={
    at={(-0.05,0.99)},anchor=east,inner sep=0pt
},
scale ticks above exponent={2}
]
\nextgroupplot
\addplot[only marks] coordinates{(1,1200) (3,500)};
\nextgroupplot
\addplot[only marks] coordinates{(2,1200) (4,400)};
\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}

答案2

仅作为一种解决方法:您可以scaled y ticks=true手动设置左侧图的选项:

\documentclass{scrartcl}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{groupplot}[
  group style={
    group size=4 by 4, %4 column(s), 4 row(s)% 
    horizontal sep=0cm,
    vertical sep=1.5cm,
    ylabels at=edge left,
    yticklabels at=edge left,
  },
  xlabel={Bla},
  ylabel={Process},
  %
  x tick scale label style={
    at={(0.95,0)},yshift=-1.5em,anchor=north,inner sep=0pt
  },
  y tick scale label style={
    at={(-0.05,0.99)},anchor=east,inner sep=0pt,
  },
  scale ticks above exponent={2},
  scaled y ticks=false% <-
]
\nextgroupplot[scaled y ticks=true]% <-
\addplot[only marks] coordinates{(1,1200) (3,500)};
\nextgroupplot
\addplot[only marks] coordinates{(2,1200) (4,400)};
\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

但是 groupplot 中还存在其他问题:左侧图和右侧图的刻度位置不同。groupplot 比 更宽\linewidth。页面上没有足够的空间容纳 4x4 groupplot。此外,我会在两个图之间插入一个小的水平分隔符。

\documentclass{scrartcl}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{groupplots}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{groupplot}[
  group style={
    group size=2 by 2, %2 column(s), 2 row(s)% <-
    horizontal sep=5pt,
    vertical sep=1.5cm,
    ylabels at=edge left,
    yticklabels at=edge left,
  },
  width=.55\linewidth,% <-
  xlabel={Bla},
  ylabel={Process},
  %
  x tick scale label style={
    at={(0.95,0)},yshift=-1.5em,anchor=north,inner sep=0pt
  },
  y tick scale label style={
    at={(-0.05,0.99)},anchor=east,inner sep=0pt,
  },
  ymin=400,ymax=1200,enlarge y limits=.1,% <-
  scale ticks above exponent={2},
  scaled y ticks=false% <-
]
\nextgroupplot[scaled y ticks=true]% <-
\addplot[only marks] coordinates{(1,1200) (3,500)};
\nextgroupplot
\addplot[only marks] coordinates{(2,1200) (4,400)};
\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

相关内容