將文字新增至表格中的明確元節點標籤

將文字新增至表格中的明確元節點標籤

我在表格中有一些元數據,我將它們作為條形標籤包含在我的圖中。我想指定這些標籤是計時的,例如 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}

MWE輸出

答案1

的預設值node near coords\pgfmathprintnumber\pgfplotspointmeta:它只是將點元值列印為數字。但它會在 Ti 內完成kZ 節點,因此您只需向其添加文字即可,它也會進入該節點。所以,你想做的事情都可以透過 來實現nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}~secs}。但是,如果您這樣做,您會看到一些“秒”延伸到上軸邊框之上,因此您還需要添加類似enlarge y limits={upper, abs value=20}.這兩項更改將為您提供以下輸出:

在此輸入影像描述

然而,就我個人而言,我寧願使用第二個符號,如下所示siunitxs

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

請注意xtick distance=1ytick 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}

在此輸入影像描述

相關內容