我无法移动条形图中的标签 {1, ..., 6}。我想将每个标签精确地定位在每个相应条形下方的中心,但我不知道该怎么做。
这是我目前正在使用的代码:
\begin{figure}[h]
\caption{\textbf{Tid det tar för brustabletten att lösas upp i de olika vätskorna}}
\label{fig:experiment}
\centering
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0,
xtick={1,2,3,4,5,6},
xticklabels={1,2,3,4,5,6},
xlabel=Experiment,
ylabel=Tid (s),
bar width=0.5cm,
nodes near coords,
nodes near coords align={vertical},
every node near coord/.append style={font=\tiny},
axis lines*=left,
enlarge x limits=0.5,
legend style={at={(0.5,-0.2)}, anchor=north,legend columns=-1},
xticklabel style={text width=1.5cm,align=center}
]
\addplot[fill=red!50] coordinates {(1,157)};
\addplot[fill=blue!50] coordinates {(2,173)};
\addplot[fill=green!50] coordinates {(3,150)};
\addplot[fill=orange!50] coordinates {(4,134)};
\addplot[fill=purple!50] coordinates {(5,80)};
\addplot[fill=brown!50] coordinates {(6,123)};
\legend{Kallt vatten, Kall läsk, Läsk utan kolsyra, Varm läsk utan kolsyra, Varmt vatten, Varm läsk}
\end{axis}
\end{tikzpicture}
\end{figure}
如能得到有关如何实现此目标的任何帮助或建议,我们将不胜感激。
答案1
您需要将(默认值)设置bar shift
为零:
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines*=left,
%
ybar,
bar width=6mm,
bar shift=0mm, % <---
ymin=0,
xtick={1,2,...,6},
tick label style={font=\footnotesize},
%
xlabel=Experiment,
ylabel=Tid (s),
%
nodes near coords,
nodes near coords align={vertical},
every node near coord/.append style={font=\tiny},
legend style={at={(0.5,-0.2)},
anchor=north, legend columns=3, font=\footnotesize,
cells={anchor=west},
/tikz/every even column/.append style={column sep=1em}},
]
\addplot[fill=red!50] coordinates {(1,157)};
\addplot[fill=blue!50] coordinates {(2,173)};
\addplot[fill=green!50] coordinates {(3,150)};
\addplot[fill=orange!50] coordinates {(4,134)};
\addplot[fill=purple!50] coordinates {(5, 80)};
\addplot[fill=brown!50] coordinates {(6,123)};
\legend{Kallt vatten, Kall läsk, Läsk utan kolsyra, Varm läsk utan kolsyra, Varmt vatten, Varm läsk}
\end{axis}
\end{tikzpicture}
\end{document}