![箱线图 - 长 xticklabels 的新线](https://linux22.com/image/476268/%E7%AE%B1%E7%BA%BF%E5%9B%BE%20-%20%E9%95%BF%20xticklabels%20%E7%9A%84%E6%96%B0%E7%BA%BF.png)
我想让我的“xticklabels”之间有一个新行,这样它们就可以分成两行,而不会太拥挤。双反斜杠 \\ 和 \n 不起作用。
代码:
\documentclass{standalone}
\usepackage{tikz,pgfplots,pgfplotstable}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=0.9\textwidth,
boxplot/draw direction=y,
axis y line=left,
enlarge y limits,
ylabel={Impact of latency},
xlabel={Game and latency},
ytick={1, 2, 3, 4, 5},
yticklabels={1, 2, 3, 4, 5},
ymajorgrids,
xtick={1, 2, 3, 4},
xticklabels={Chess 0ms , Chess 1000ms, Pong 0ms, Pong 1000ms},
]
\addplot+ [boxplot prepared={lower whisker=1, upper whisker=2}] coordinates {};
\addplot+ [boxplot prepared={lower whisker=2, upper whisker=4}] coordinates {};
\addplot+ [boxplot prepared={lower whisker=1, upper whisker=2}] coordinates {};
\addplot+ [boxplot prepared={lower whisker=4, upper whisker=5}] coordinates {};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
若要在节点内用 断线,\\
需要用 来设置其对齐方式align
。添加
xticklabel style={align=center}
你可以使用
xticklabels={Chess\\ 0ms, Chess\\ 1000ms, Pong\\ 0ms, Pong\\ 1000ms},
完整示例(我删除了axis y line=left
,它在框左侧添加了一个箭头):
\documentclass{standalone}
\usepackage{tikz,pgfplots,pgfplotstable}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=0.9\textwidth,
boxplot/draw direction=y,
enlarge y limits,
ylabel={Impact of latency},
xlabel={Game and latency},
ytick={1, 2, 3, 4, 5},
ymajorgrids,
xtick={1, 2, 3, 4},
xticklabels={Chess\\ 0ms, Chess\\ 1000ms, Pong\\ 0ms, Pong\\ 1000ms},
xticklabel style={align=center}
]
\addplot+ [boxplot prepared={lower whisker=1, upper whisker=2}] coordinates {};
\addplot+ [boxplot prepared={lower whisker=2, upper whisker=4}] coordinates {};
\addplot+ [boxplot prepared={lower whisker=1, upper whisker=2}] coordinates {};
\addplot+ [boxplot prepared={lower whisker=4, upper whisker=5}] coordinates {};
\end{axis}
\end{tikzpicture}
\end{document}