ytick 标签太小,需要分成多行

ytick 标签太小,需要分成多行

问题:我试图将 ytick 标签对齐到两行,因为标签对于数字来说看起来很小。重申一下,如下图所示,我希望在一行中输入 N=3,在另一行中添加方括号中的数字。对于 N=3、N=6、N=12、N=24、N=48 的所有 ytick 标签,应应用相同的方法。

代码:

\documentclass[10pt,journal,compsoc,xcolor={dvipsnames}]{IEEEtran}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\usepackage{pgfplotstable}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{multirow}
\usepackage{lipsum}
\usepackage{xspace}
\usetikzlibrary{patterns}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\newcommand{\R}{\mathbb{R}}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=0.7pt] (char) {#1};}}
% \usetikzlibrary{patterns, patterns.meta}
% \usepackage{amsmath, amssymb}
\begin{document}
\begin{figure*}[h]
\begin{tikzpicture}
\begin{groupplot}[
group style={group size=3 by 1, horizontal sep=1.5cm}, 
width=0.29\textwidth, height=0.3cm,
xmajorgrids=true,
tick align=outside, xtick pos=left,
scale only axis,
x post scale=0.85,
enlarge y limits=0.2,
xmin=0, xmax=100,
xtick={0,20,...,100},
ytick=data, %yticklabels={N=3,N=6,N=12,N=24,N=48},
xticklabel={\pgfmathprintnumber{\tick}\%},
xticklabel style={font=\scriptsize},
yticklabel style={font=\tiny, xshift=3pt},
nodes near coords style={font=\scriptsize,  yshift=8pt},
legend style={at={(0.5,1.05)}, anchor=south, legend columns=4, font=\footnotesize, cells={anchor=west}},
xbar stacked,
/pgf/bar width=2.5mm,
y=5.2mm,
legend image code/.code={%
            \draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.2cm);
            },
title style={at={(0.5,-0.3)}, anchor=north},
nodes near coords={\pgfkeys{/pgf/number format/precision=0}\pgfmathprintnumber{\pgfplotspointmeta}\%},
%coordinate style/.condition={x-0.5*rawx<5}{red, xshift=4pt},
coordinate style/.condition={x-0.5*rawx>95}{xshift=-4pt},
]

\nextgroupplot[title=(a) MADDPG,
yticklabels={N=3~[3824s],N=6~[11134s],N=12~[33443s],N=24~[113706s],N=48~[432054s]}]
\addplot [color=black, pattern color=black, pattern=crosshatch dots, %
nodes near coords={\pgfmathparse{less(\pgfplotspointmetatransformed,100)}%
\ifnum\pgfmathresult=1%
\hspace{8pt}%
\fi%
\pgfkeys{/pgf/number format/precision=0}\pgfmathprintnumber{\pgfplotspointmeta}\%}]
coordinates{(43,1) (30,2) (20,3) (12,4) (4,5)};
%\addplot [color=black,  pattern color=gray, pattern=checkerboard] 
%coordinates{(16.57,1) (39.97,2) (56.37,3) (0,4)};
\addplot [color=black,  pattern color=gray, pattern=checkerboard] coordinates{(55,1) (68,2)(79,3) (88,4) (96,5)};
\addplot [color=black,  pattern color=black, pattern=north west lines] coordinates{(2,1) (2,2) (1,3) (1,4) (0,5)};
%4
\nextgroupplot[title=(b) MATD3,
yticklabels={N=3~[3536s],N=6~[10719s],N=12~[34193s],N=24~[115256s],N=48~[427447s]}]
\addplot [color=black,  pattern color=black, pattern=crosshatch dots, %
nodes near coords={\pgfmathparse{less(\pgfplotspointmetatransformed,100)}%
\ifnum\pgfmathresult=1%
\hspace{8pt}%
\fi%
\pgfkeys{/pgf/number format/precision=0}\pgfmathprintnumber{\pgfplotspointmeta}\%}]
coordinates{(45,1) (31,2) (18,3) (10,4) (3,5)};
% \addplot [color=black, pattern color=gray, pattern=checkerboard] 
% coordinates{(22,1) (36,2) (50.12,3) (0,4)};
\addplot [color=black,  pattern color=gray, pattern=checkerboard] 
coordinates{(53,1) (67,2) (81,3) (90,4) (97,5)};
\addplot [color=black, pattern color=black, pattern=north west lines] 
coordinates{(2,1) (2,2) (1,3) (0,4) (0,5)};
\legend{Action selection, Update all trainers, Other Parts},
%3,4

\nextgroupplot[title=(c) MASAC,
yticklabels={N=3~[4987s],N=6~[14979s],N=12~[47857s],N=24~[172571s],N=48~[679289s]}]
\addplot [color=black,  pattern color=black, pattern=crosshatch dots]
coordinates{(52,1) (45,2) (38,3) (26,4) (13,5)};
% \addplot [color=black, pattern color=gray, pattern=checkerboard] 
% coordinates{(21,1) (36,2) (51,3) (0,4)};
\addplot [color=black,   pattern color=gray, pattern=checkerboard] coordinates{(46,1) (53,2)(62,3) (74,4) (87,5)};
\addplot [color=black, pattern color=black, pattern=north west lines] 
coordinates{(2,1) (2,2) (0,3) (0,4) (0,5)};
% %3,4

\end{groupplot}
\end{tikzpicture}
%\vspace{-\baselineskip}
\caption{algos}
\label{figure1}
\end{figure*}
\end{document}

输出: 在此处输入图片描述

答案1

  • 看来你的这个问题是对你之前问题的后续,你接受了(我的)答案,它的代码更清晰、简洁,结构也更好,就像你问题中的 MWE 一样。我想知道,你为什么不使用它呢?
  • 请在 MWE 问题中删除所有注释代码行。它们不会让问题更清晰,在我看来,只会分散读者的注意力。
  • 要用两行写,yticklabels您需要定义它们的样式,例如yticklabel style={align=right}
  • 为了使两张标签不互相重叠,合理的方法是使条形图变粗,标签的基线变小。
  • 完整的 MWE,基于我的答案关于你上一个问题:
\documentclass[journal,compsoc,xcolor={dvipsnames}]{IEEEtran}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{patterns, patterns.meta}

\begin{document}
    \begin{figure*}
    \raggedright
\begin{tikzpicture}[
patt-A/.style = {pattern={Dots[angle=45, distance=2pt,xshift=1pt]},
                 pattern color=#1},
patt-B/.style = {pattern=checkerboard,
                 pattern color=#1},
patt-C/.style = {pattern={Lines[angle=45,distance=2pt]},
                 pattern color=#1},
                    ]
    \begin{groupplot}[
group style={
    group size=3 by 1,
    horizontal sep=16mm     % <---
            },
    width=0.33\linewidth,
    enlarge y limits=0.2,
%
    xmajorgrids=true,
    tick align=outside,
%
    title style={at={(0.5,-0.3)}, anchor=north},
%
    xbar stacked,
    /pgf/bar width=4mm,
    y=8mm,
%
    xmin=0, xmax=100,
    xtick distance=20,
    xticklabel={\pgfmathprintnumber{\tick}\%},
    ticklabel style={font=\scriptsize\linespread{0.84}\selectfont}, % <---
    yticklabel style={align=right},   % <---
    ytick=data,
%
    nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%
                   },
coordinate style/.condition={x-0.5*rawx<11}{xshift=+2pt},
coordinate style/.condition={x-0.5*rawx>95}{xshift=-4pt},
    every node near coord/.append style={font=\tiny, yshift=2.2ex},
%
legend image code/.code={\draw[#1] (0cm,-0.1cm) rectangle (0.4cm,0.1cm);},
legend style={at={(0.5,1.05)}, anchor=south,
              legend columns=-1,
              font=\scriptsize,
              /tikz/every even column/.append style={column sep=1em}
              },
]

\nextgroupplot[
    title=(a) MADDPG,
    yticklabels={N=3\\{[3824s]},N=6\\{[11134s]},N=12\\{[33443s]},
                 N=24\\{[113706s]},N=48\\{[432054s]}}
                ]
\addplot [patt-A=gray]    coordinates{(43,1) (30,2) (20,3) (12,4) (4,5)};
\addplot [patt-B=gray]    coordinates{(55,1) (68,2) (79,3) (88,4) (96,5)};
\addplot [patt-C=black]   coordinates{(2,1)  (2,2)  (1,3)  (1,4)  (0,5)};

\nextgroupplot[
    title=(b) MATD3,
yticklabels={N=3\\{[3536s]},N=6\\{[10719s]},N=12\\{[34193s]},
             N=24\\{[115256s]},N=48\\{[427447s]}}
                ]
\addplot [patt-A=gray]
    coordinates{(45,1) (31,2) (18,3) (10,4) (3,5)};
\addplot [patt-B=gray]
    coordinates{(53,1) (67,2) (81,3) (90,4) (97,5)};
\addplot [patt-C=black]
    coordinates{(2,1) (2,2) (1,3) (0,4) (0,5)};
\legend{Action selection, Update all trainers, Other Parts},

\nextgroupplot[
    title=(c) MASAC,
    yticklabels={N=3\\{[4987s}],N=6\\{[14979s]},
                 N=12\\{[47857s]},N=24\\{[172571s]},N=48\\{[679289s]}}
                ]
\addplot [patt-A=black]
    coordinates{(52,1) (45,2) (38,3) (26,4) (13,5)};
\addplot [patt-B=gray]
    coordinates{(46,1) (53,2)(62,3) (74,4) (87,5)};
\addplot [patt-C=gray]
    coordinates{(2,1) (2,2) (0,3) (0,4) (0,5)};
\end{groupplot}
\end{tikzpicture}

\caption{Training time breakdown for three MARL workloads with 3, 6, 12 \& 24 agents. The environment is Competitive task~(Predator-Prey).}
\label{figure1}
    \end{figure*}
\end{document}
  • % <---与我之前的回答相比,代码中标记了变化。

在此处输入图片描述

  • 不清楚第二行标签中的数据是什么意思。您可以考虑使用siunitx包并将它们(例如图 a)写成:
    yticklabels={N=3\\{[\qty{3824}{s}]},N=6\\{[\qty{11134}{s}]},N=12\\{[\qty{33443}{s}]},
                 N=24\\{[\qty{113706}{s}]},N=48\\{[\qty{432054}{s}]}}

将提供更好的格式:

在此处输入图片描述

相关内容