
テーブルにいくつかのメタデータがあり、それをプロットのバー ラベルとして含めています。これらのラベルがタイミング (例: 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}
。ただし、これを行うと、いくつかの「secs」が上軸の境界より上に伸びることがわかるので、 のようなものも追加する必要がありますenlarge y limits={upper, abs value=20}
。この 2 つの変更により、次の出力が得られます。
しかし、個人的には、次のように秒を表す記号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}