如何要求 pgfplot 将太长的标签分成多行?
例如,我有一张图表,描述了一份问卷的答案。问题应该是 Y 轴的标签,用ticklabels
PDFPlots 的术语来说。然而,许多问题都很长,这些标签的空间应该限制在 5 厘米以内。
这是我目前的尝试。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.17}
\begin{document}
\pgfplotstableread[col sep=&]{
Q & A & B & C
Q1 Very long question text that should be split in multiple lines & 0 & 1 & 3
Q2 Shorter question & 0 & 2 & 2
Q3 Another question with a long text & 2 & 1 & 1
}{\answers}
\begin{tikzpicture}
\begin{axis}[
xbar stacked,
y=0.8cm,
y dir=reverse,
xmin=0, xmax=4,
ytick=data,
yticklabels from table={\answers}{Q},
yticklabel style={font=\footnotesize, width=5cm}, % << ERROR: width is not a recognized tikz key
]
\addplot [fill=red] table [x=A, y expr=\coordindex] {\answers};
\addplot [fill=yellow] table [x=B, y expr=\coordindex] {\answers};
\addplot [fill=green] table [x=C, y expr=\coordindex] {\answers};
\end{axis}
\end{tikzpicture}
\end{document}
该文档无法编译并出现以下错误:Package pgfkeys Error: I do not know the key '/tikz/width', to which you passed '5cm', and I am going to ignore it.
有没有办法强制每个刻度标签都有设定的宽度?
答案1
pgfplots
刻度标签是tikz
节点,这里需要选项text width=<dimension>
。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.17}
\begin{document}
\pgfplotstableread[col sep=&]{
Q & A & B & C
Q1 Very long question text that should be split in multiple lines & 0 & 1 & 3
Q2 Shorter question & 0 & 2 & 2
Q3 Another question with a long text & 2 & 1 & 1
}{\answers}
\begin{tikzpicture}
\begin{axis}[
xbar stacked,
y=0.8cm,
y dir=reverse,
xmin=0, xmax=4,
ytick=data,
yticklabels from table={\answers}{Q},
yticklabel style={font=\footnotesize, text width=5cm}
]
\addplot [fill=red] table [x=A, y expr=\coordindex] {\answers};
\addplot [fill=yellow] table [x=B, y expr=\coordindex] {\answers};
\addplot [fill=green] table [x=C, y expr=\coordindex] {\answers};
\end{axis}
\end{tikzpicture}
\end{document}
使用选项text width
或align=left|center|right
,您还可以通过插入来手动指定换行符\\
。
答案2
我创建一个新列,将 Q 列的内容嵌入其中parbox
。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.17}
\begin{document}
\pgfplotstableread[col sep=&]{
Q & A & B & C
Q1 Very long question text that should be split in multiple lines & 0 & 1 & 3
Q2 Shorter question & 0 & 2 & 2
Q3 Another question with a long text & 2 & 1 & 1
}{\answers}
\pgfplotstablecreatecol[create col/assign/.code={%
\getthisrow{Q}\entry%
\edef\entry{\noexpand\parbox{5cm}{\entry}}%
\pgfkeyslet{/pgfplots/table/create col/next content}\entry%
}]{Qwidth}{\answers}
\begin{tikzpicture}
\begin{axis}[
xbar stacked,
y=0.8cm,
y dir=reverse,
xmin=0, xmax=4,
ytick=data,
yticklabels from table={\answers}{Qwidth},
yticklabel style={font=\footnotesize},
]
\addplot [fill=red] table [x=A, y expr=\coordindex] {\answers};
\addlegendentry{A}
\addplot [fill=yellow] table [x=B, y expr=\coordindex] {\answers};
\addlegendentry{B}
\addplot [fill=green] table [x=C, y expr=\coordindex] {\answers};
\addlegendentry{C}
\end{axis}
\end{tikzpicture}
\end{document}