![箱線圖 - 長 xticklabel 的新線](https://rvso.com/image/476268/%E7%AE%B1%E7%B7%9A%E5%9C%96%20-%20%E9%95%B7%20xticklabel%20%E7%9A%84%E6%96%B0%E7%B7%9A.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}