![PGFPlots:使条形图中的条形在条形顶部共享值](https://linux22.com/image/465765/PGFPlots%EF%BC%9A%E4%BD%BF%E6%9D%A1%E5%BD%A2%E5%9B%BE%E4%B8%AD%E7%9A%84%E6%9D%A1%E5%BD%A2%E5%9C%A8%E6%9D%A1%E5%BD%A2%E9%A1%B6%E9%83%A8%E5%85%B1%E4%BA%AB%E5%80%BC.png)
我有一个 PGF 条形图:
\documentclass[]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
ybar, axis on top,
bar width=0.4cm,
ymax=30,
width=5.8cm,
ymajorgrids, tick align=inside,
major grid style={draw=white},
enlarge y limits={value=.1,upper},
ymin=0,
tickwidth=0pt,
enlarge x limits=0.23,
legend style={
at={(0.5,-0.2)},
anchor=north,
legend columns=-1,
/tikz/every even column/.append style={column sep=0.5cm}
},
yticklabels={,,},
x tick label style={font=\scriptsize,text width=1.5cm,align=center},
symbolic x coords={
Traditional,
Optimized,
CBOR encoded},
xtick=data,
nodes near coords={
\pgfmathprintnumber[precision=2]{\pgfplotspointmeta}
}
]
\addplot [draw=none, fill=yellow!30] coordinates {
(Traditional, 27.77)
(Optimized, 26.43)
(CBOR encoded, 18.51)
};
\addplot [draw=none,fill=yellow!50] coordinates {
(Traditional, 27.77)
(Optimized, 23.14)
(CBOR encoded, 13.88)
};
\legend{Client, Master}
\end{axis}
\end{tikzpicture}
\end{document}
看起来像这样:
显然,前两个条形图重叠的情况看起来不太好,因为它们共享相同的值(27.77)
我怎样才能让它们共享一个值,像这样?(通过编辑 PDF 实现)
请注意,我不想编辑 PDF,因为我希望图表在不同的文档格式之间兼容。
答案1
通过添加单独的节点并使用以下方法修复\usetikzlibrary{calc}
:
\documentclass[]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
ybar, axis on top,
bar width=0.4cm,
ymax=30,
width=5.8cm,
ymajorgrids, tick align=inside,
major grid style={draw=none},
enlarge y limits={value=.1,upper},
ymin=0,
tickwidth=0pt,
enlarge x limits=0.23,
legend style={
at={(0.5,-0.2)},
anchor=north,
legend columns=-1,
/tikz/every even column/.append style={column sep=0.5cm}
},
yticklabels={,,},
x tick label style={font=\scriptsize,text width=1.5cm,align=center},
symbolic x coords={
Traditional,
Optimized,
CBOR encoded},
xtick=data,
% nodes near coords={
% \pgfmathprintnumber[precision=2]{\pgfplotspointmeta}
% }
]
\addplot [draw=none, fill=yellow!30] coordinates {
(Traditional, 27.77)
(Optimized, 26.43)
(CBOR encoded, 18.51)
};
\addplot [draw=none,fill=yellow!50] coordinates {
(Traditional, 27.77)
(Optimized, 23.14)
(CBOR encoded, 13.88)
};
\node[above] at ($(axis cs:Traditional,27.77)+(0,0)$) {27.77};
\node[above] at ($(axis cs:Optimized,26.43)+(-17,0)$) {26.43};
\node[above] at ($(axis cs:Optimized,23.14)+(17,0)$) {23.14};
\node[above] at ($(axis cs:CBOR encoded,18.51)+(-17,0)$) {18.51};
\node[above] at ($(axis cs:CBOR encoded,13.88)+(17,0)$) {13.88};
\legend{Client, Master}
\end{axis}
\end{tikzpicture}
\end{document}