0

I'm trying to create a graph like this:

wanted

But I can only get it to look like this using legend text, but I want to put the text directly below the bar graphs (like 0ms, 50ms, 150ms)

current

My code:

    \begin{figure}[ht]
    \centering
    \begin{tikzpicture}
    \begin{axis}[
        ybar,
        bar width=20pt,
        width=0.5\textwidth,
        height=0.5\textwidth,
        enlarge x limits=0.5, % dist between x bars
        legend style={at={(0.5,-0.20)},anchor=north,legend columns=-1},
        ylabel={Impact of latency on gameplay, 1-5},
        symbolic x coords={Chess, Pong},
        xtick=data,
        ymin=1,ymax=5,
        nodes near coords,
        nodes near coords align={vertical},
        ]
    \addplot coordinates {(Chess,1.1) (Pong,1.5)}; % 0ms results
    \addplot coordinates {(Chess,1.6) (Pong,4.6)}; % 1000ms results
    \legend{100ms delay,1000ms delay}
    \end{axis}
    \end{tikzpicture}
    \end{figure}
1
  • 1
    Hm, all information is there, via the legend. I'd move it perhaps inside the image to the upper left ...
    – MS-SPO
    Commented Apr 24 at 15:51

1 Answer 1

1

A little complex, but it works. Manually add extra ticks at normalized symbolic x coordinates for individual bar. Chess will be the [normalized]0 coordinate. Pong will be the [normalized]1 coordinate. Set xmin to [normalized]-0.5 and xmax to [normalized]1.5. Then you could locate the individual bar coordinate manually to add the extra ticks. There might be better solutions:

\documentclass{report}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar=5pt,
bar width=20pt,
width=0.5\textwidth,
height=0.5\textwidth,
ylabel={Impact of latency on gameplay, 1-5},
symbolic x coords={Chess, Pong},
xtick=data,
tick style={draw=none},
xticklabel style={yshift=-0.5cm},
extra x ticks={
[normalized]-0.2,
[normalized]0.2,
[normalized]0.8,
[normalized]1.2
},
extra x tick style={
tick style={draw=none},
tick label style={
yshift=0.5cm,
font=\tiny,
}
},
extra x tick labels={100ms,1000ms,100ms,1000ms},
xmin={[normalized]-0.5},xmax={[normalized]1.5},
ymin=1,ymax=5,
nodes near coords
]
\addplot coordinates {(Chess,1.1) (Pong,1.5)}; % 0ms results
\addplot coordinates {(Chess,1.6) (Pong,4.6)}; % 1000ms results
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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