
Tengo algunos metadatos en una tabla que incluyo como etiquetas de barras en mi gráfico. Me gustaría especificar que estas etiquetas son tiempos, por ejemplo, 15,8 segundos. No sé cómo hacer que se agregue el texto "segundos" a las etiquetas.
Aquí hay un 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}
Respuesta1
El valor predeterminado de node near coords
es \pgfmathprintnumber\pgfplotspointmeta
: simplemente imprime el metavalor del punto como un número. Pero lo hace dentro de un TikNodo Z, por lo tanto, simplemente puede agregarle texto y también irá al nodo. Entonces, lo que quieres hacer se puede lograr con nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}~secs}
. Sin embargo, si haces esto, verás que algunos de los “segundos” se extienden por encima del borde del eje superior, por lo que también querrás agregar algo como enlarge y limits={upper, abs value=20}
. Estos dos cambios le darán el siguiente resultado:
Sin embargo, personalmente, prefiero usar siunitx
y el s
símbolo para el segundo, así:
nodes near coords={%
${\pgfmathprintnumber\pgfplotspointmeta} \, \si{\second}$%
}
Tenga en cuenta xtick distance=1
y ytick distance=50
que simplifican un poquito su código. Además, suelo \pgfplotsset{compat=1.16}
asegurarme de que el resultado siga siendo el mismo incluso en versiones futuras de 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}