1

I want to make my "xticklabels" have a new line between them so that they are in two lines and not as cramped. The double backslash \\ and \n did not work.

my graph

Code:

\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
  • Welcome to TeX.SE!
    – Mensch
    Commented May 2 at 15:25

1 Answer 1

1

To break lines with \\ in nodes you need to set its alignment with align. Add

xticklabel style={align=center}

and you can use

xticklabels={Chess\\ 0ms,  Chess\\ 1000ms, Pong\\ 0ms, Pong\\ 1000ms},

Full example (I removed axis y line=left, it add an arrows on the left side of the box):

\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}

Full example

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .