I have two groups of data, you can think of them as one control group and one treatment group, and each data point shows the system's performance under certain parameter x.
Now for each value of parameter x, I want to draw two bars (one for the control group and the other for the treatment group) that stays closely together, to compare their performance. This is already done.
Furthermore, I want to draw one node per two bars, on top of the two bars, to indicate the ratio of the treatment group's performance to control group's.
Note that the red text above each pair of bars are the effect I want. Is this doable in pgfplots?
I have looked at bar shift
and bar near coords
but still does not get a succinct way to do what I want.
Specifically, the ideal case is that I provide the two groups of data to pgfplots, like
\addplot coordinates { (1,x1) (2,x2) ... (9,x9) };
\addplots coordinates { (1,y1) (2,y2) ... (9,y9) };
and some command automatically generates the nodes (y1/x1) (y2/x2) … (y9/x9) on top of each pair of bars.
Thanks in advance!
The minimal code generating the diagram similar to that in the question is shown below. I have removed minor things like legend and patterns.
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{semilogyaxis}[
xtick = data,
symbolic x coords={1,2,4,8,16,32,64,128,256,512,1024,2048},
x tick label style={rotate=45,anchor=east,xshift=0.5em},
ybar=0pt,
bar width=5pt,
]
\addplot
coordinates {
(1, 12224822.47615195) (2, 30575568.537983067) (4, 56065152.09279688) (8, 119050256.55456343) (16, 261199511.2399962) (32, 539436878.3711921) (64, 884494633.5530908) (128, 1458149738.5198479) (256, 1504651025.9424655) (512, 3690489159.8036933) (1024, 4002894124.741196) (2048, 5886439904.74722)
};
\addplot
coordinates {
(1, 12120350.197539225) (2, 30647908.833372578) (4, 56033201.34011042) (8, 118156559.2107797) (16, 258243240.89016733) (32, 538593063.3199656) (64, 853738697.5167842) (128, 1323350550.6813228) (256, 1364824842.2256825) (512, 3354827274.6800394) (1024, 4111728131.806063) (2048, 6950375713.147153)
};
\end{semilogyaxis}
\end{tikzpicture}
\end{document}
Best Answer
As already stated in the comments below the question, you can use the
nodes near coords
feature to plot the numbers above the bars. Here a complete solution. For more details on how it works, please have a look at the comments in the code.