Hinzufügen von Text zu expliziten Metaknotenbeschriftungen aus der Tabelle

Hinzufügen von Text zu expliziten Metaknotenbeschriftungen aus der Tabelle

Ich habe einige Metadaten in einer Tabelle, die ich als Balkenbeschriftungen in mein Diagramm einfüge. Ich möchte angeben, dass diese Beschriftungen Zeitangaben sind, z. B. 15,8 Sekunden. Ich weiß nicht, wie ich den Text „Sekunden“ an die Beschriftungen anhängen kann.

Hier ist ein 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}

MWE-Ausgabe

Antwort1

Der Standardwert von node near coordsist \pgfmathprintnumber\pgfplotspointmeta: es druckt einfach den Punktmetawert als Zahl. Aber es tut dies innerhalb eines TikZ-Knoten, daher können Sie einfach Text hinzufügen und dieser wird auch in den Knoten eingefügt. Was Sie also tun möchten, können Sie mit erreichen nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}~secs}. Wenn Sie dies jedoch tun, werden Sie feststellen, dass einige der „Sekunden“ über den oberen Achsenrand hinausragen, daher sollten Sie auch etwas wie hinzufügen enlarge y limits={upper, abs value=20}. Diese beiden Änderungen führen zu folgender Ausgabe:

Bildbeschreibung hier eingeben

Persönlich würde ich jedoch für das zweite lieber siunitxund das sSymbol verwenden, und zwar wie folgt:

nodes near coords={%
  ${\pgfmathprintnumber\pgfplotspointmeta} \, \si{\second}$%
}

Beachten Sie die xtick distance=1und ytick distance=50, die Ihren Code ein wenig vereinfachen. Außerdem verwende ich , \pgfplotsset{compat=1.16}um sicherzustellen, dass die Ausgabe auch in zukünftigen Versionen von gleich bleibt 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}

Bildbeschreibung hier eingeben

verwandte Informationen