如何在多行中显示 xtick 标签?

如何在多行中显示 xtick 标签?

我的条形图有以下代码。

\documentclass[12pt,a4paper,oneside]{report}
\usepackage{pgfplots} 
\usepgfplotslibrary{colorbrewer}
\pgfplotsset{width=14cm,compat=1.9}
\pgfplotstableread[row sep=\\,col sep=&]{
    Category&2007&2008&2009\\
        CSSSSSSSSSSSSSSSSSSSSSSS  & 29 & 22   & 32 \\
        CROsssssssssssssssssss & 20 & 11   & 14\\
        NCO & 23 & 20   & 28\\
        Ph  & 13 & 23   & 10 \\
        IS  & 52 & 32   & 38 \\
        L   & 8  & 21   & 11 \\
        NUK & 4  & 7    & 9 \\
        IS1  & 52 & 32   & 33\\
        L1  & 8  & 21   & 11 \\
        NUK1 & 4  & 7    & 9 \\
}\mydata


\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
symbolic x coords={CSSSSSSSSSSSSSSSSSSSSSSS,CROsssssssssssssssssss,NCO,Ph,IS,L,NUK,IS1,L1,NUK1},
x tick label style={align=center},
xtick=data,
% reduce height of axis a bit
height=9cm,
% set width of bars
bar width=4pt,
% remove gap below bars
ymin=0,
enlarge x limits=.3,
% remove frame around legend, add some space
legend style={draw=none,column sep=2mm},
legend columns=2,
% customize how the legend images are drawn
% draw a square instead of two bars
legend image code/.code={%
       \draw[#1,draw=none,/tikz/.cd,yshift=-0.25em]
        (0cm,1pt) rectangle (6pt,7pt);},
% color map from colorbrewer
cycle list/Paired,
% the above only sets the color, need to specify that bars should be filled
every axis plot/.append style={fill}
]
\addplot table[x=Category,y=2007]{\mydata};
\addplot table[x=Category,y=2008]{\mydata};
\addplot table[x=Category,y=2009]{\mydata};


\legend{2007,2008,2009}
\end{axis}
\end{tikzpicture}
\end{document}

我得到的输出如下

在此处输入图片描述

xlabels CSSSSSSSSSSSSSSSSSSSSSSSS、CROssssssssssssssssssss 重叠。如何将它们分成多行,以使它们不重叠?

答案1

如果您确实想要,请引入空格断点并设置一些text width。我没有耐心调整宽度直到所有\underfull投诉\overfull都消失。

\documentclass[12pt,a4paper,oneside]{report}
\usepackage{pgfplots} 
\usepgfplotslibrary{colorbrewer}
\pgfplotsset{width=14cm,compat=1.9}
\pgfplotstableread[row sep=\\,col sep=&]{
    Category&2007&2008&2009\\
        CSS SSS SSS SSS SSS SSS SSS SSS & 29 & 22   & 32 \\
        CRO sss sss sss sss sss sss s & 20 & 11   & 14\\
        NCO & 23 & 20   & 28\\
        Ph  & 13 & 23   & 10 \\
        IS  & 52 & 32   & 38 \\
        L   & 8  & 21   & 11 \\
        NUK & 4  & 7    & 9 \\
        IS1  & 52 & 32   & 33\\
        L1  & 8  & 21   & 11 \\
        NUK1 & 4  & 7    & 9 \\
}\mydata

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
symbolic x coords={CSS SSS SSS SSS SSS SSS SSS SSS,CRO sss sss sss sss sss sss s,%
NCO,Ph,IS,L,NUK,IS1,L1,NUK1},
x tick label style={align=center},
xtick=data,
% reduce height of axis a bit
height=9cm,
% set width of bars
bar width=4pt,
% remove gap below bars
ymin=0,
enlarge x limits=.12,
% remove frame around legend, add some space
legend style={draw=none,column sep=2mm},
legend columns=2,
% customize how the legend images are drawn
% draw a square instead of two bars
legend image code/.code={%
       \draw[#1,draw=none,/tikz/.cd,yshift=-0.25em]
        (0cm,1pt) rectangle (6pt,7pt);},
% color map from colorbrewer
cycle list/Paired,
% the above only sets the color, need to specify that bars should be filled
every axis plot/.append style={fill},
xticklabel style={align=center,text width=6mm}
]
\addplot table[x=Category,y=2007]{\mydata};
\addplot table[x=Category,y=2008]{\mydata};
\addplot table[x=Category,y=2009]{\mydata};


\legend{2007,2008,2009}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容