箱線圖 - 長 xticklabel 的新線

箱線圖 - 長 xticklabel 的新線

我想讓我的“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}

完整範例

相關內容