![在 TikzPicture 中的每个栏下方放置标签](https://linux22.com/image/475940/%E5%9C%A8%20TikzPicture%20%E4%B8%AD%E7%9A%84%E6%AF%8F%E4%B8%AA%E6%A0%8F%E4%B8%8B%E6%96%B9%E6%94%BE%E7%BD%AE%E6%A0%87%E7%AD%BE.png)
我正在尝试创建这样的图表:
但是我只能使用图例文本使其看起来像这样,但我想将文本直接放在条形图下方(例如 0ms、50ms、150ms)
我的代码:
\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
有点复杂,但确实有效。在单个条形的标准化符号 x 坐标处手动添加额外的刻度。Chess
将是 [标准化]0 坐标。Pong
将是 [标准化]1 坐标。设置xmin
为 [标准化]-0.5 和xmax
[标准化]1.5。然后,您可以手动找到单个条形坐标以添加额外的刻度。可能有更好的解决方案:
\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}