
У меня есть некоторые метаданные в таблице, которые я включаю в качестве меток столбцов в свой график. Я хотел бы указать, что эти метки являются таймингами, например, 15,8 сек. Я не знаю, как добавить текст «сек» к меткам.
Вот MWE:
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfplotstableread{
c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11
1 94 0 5 6 6 15.8 2.0 37.5 42.3 42.3
2 93 0 5 7 7 16.1 2.1 30.7 43.1 42.5
3 97 0 11 13 13 18.6 1.9 39.0 51.6 51.9
4 87 34 93 93 93 34.6 34.6 93.8 93.3 92.4
}\inittable
\begin{tikzpicture}
\begin{axis}[
ybar, bar width=10pt,
width=6in, height=2.5in,
ymin=-5, ymax=120, ytick={0,50,100},
xmin=0, xmax=5, xtick={1,2,3,4},
point meta=explicit,
nodes near coords, every node near coord/.append style={
anchor= west, rotate=90, font=\footnotesize},
tick label style={font=\footnotesize},
]
\addplot table[x=c1,y=c2,meta=c7] {\inittable};
\addplot table[x=c1,y=c3,meta=c8] {\inittable};
\addplot table[x=c1,y=c4,meta=c9] {\inittable};
\addplot table[x=c1,y=c5,meta=c10] {\inittable};
\addplot table[x=c1,y=c6,meta=c11] {\inittable};
\end{axis}
\end{tikzpicture}
\end{document}
решение1
Значение по умолчанию node near coords
: \pgfmathprintnumber\pgfplotspointmeta
просто печатает мета-значение точки как число. Но это происходит в течение TiкУзел Z, поэтому вы можете просто добавить к нему текст, и он тоже войдет в узел. Итак, то, что вы хотите сделать, можно сделать с помощью nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}~secs}
. Однако, если вы сделаете это, вы увидите, что несколько «сек» выходят за верхнюю границу оси, поэтому вам также нужно будет добавить что-то вроде enlarge y limits={upper, abs value=20}
. Эти два изменения дадут вам следующий вывод:
Однако лично я бы предпочел использовать символ siunitx
и s
для второго варианта, например:
nodes near coords={%
${\pgfmathprintnumber\pgfplotspointmeta} \, \si{\second}$%
}
Обратите внимание на xtick distance=1
и ytick distance=50
, которые немного упрощают ваш код. Также я использую, \pgfplotsset{compat=1.16}
чтобы убедиться, что вывод останется прежним даже в будущих версиях pfgplots
.
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\pgfplotstableread{
c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11
1 94 0 5 6 6 15.8 2.0 37.5 42.3 42.3
2 93 0 5 7 7 16.1 2.1 30.7 43.1 42.5
3 97 0 11 13 13 18.6 1.9 39.0 51.6 51.9
4 87 34 93 93 93 34.6 34.6 93.8 93.3 92.4
}\inittable
\begin{tikzpicture}
\begin{axis}[
ybar, bar width=10pt,
width=6in, height=2.5in,
ymin=-5, ymax=120, ytick distance=50,
xmin=0, xmax=5, xtick distance=1,
point meta=explicit,
enlarge y limits={upper, abs value=5},
nodes near coords={$\pgfmathprintnumber{\pgfplotspointmeta} \, \si{\second}$},
every node near coord/.append style={
anchor=west, rotate=90, font=\footnotesize,
},
tick label style={font=\footnotesize},
]
\addplot table[x=c1,y=c2,meta=c7] {\inittable};
\addplot table[x=c1,y=c3,meta=c8] {\inittable};
\addplot table[x=c1,y=c4,meta=c9] {\inittable};
\addplot table[x=c1,y=c5,meta=c10] {\inittable};
\addplot table[x=c1,y=c6,meta=c11] {\inittable};
\end{axis}
\end{tikzpicture}
\end{document}