4

I want make a bar graph with clusters of two bars together, just like in the below code. However, I want to have one number centered above the two bars, instead of having two numbers above each bar. How can I do this?

MWE (taken from pgfplots manual):

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pagestyle{empty}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    x tick label style={
        /pgf/number format/1000 sep=},
    ylabel=Population,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.15)},
        anchor=north,legend columns=-1},
    ybar=5pt,% configures `bar shift'
    bar width=9pt,
    nodes near coords,
    point meta=y *10^-7 % the displayed number
]
\addplot 
    coordinates {(1930,50e6) (1940,33e6)
         (1950,40e6) (1960,50e6) (1970,70e6)};

\addplot 
    coordinates {(1930,38e6) (1940,42e6) 
        (1950,43e6) (1960,45e6) (1970,65e6)};

\legend{Far,Near}
\end{axis}
\end{tikzpicture}
\end{document}

the graph

5
  • But which number do you want to display there? How do you determine it? Feb 27, 2017 at 8:37
  • Percent difference between the two bars (which I can input manually). So there should be a percent sign after the number. Feb 27, 2017 at 9:02
  • Take the labels off the bars, and use a third add plot that just plots the numbers you want?
    – Thruston
    Feb 27, 2017 at 9:13
  • No, I want to do it like this. It should be possible in pgfplots, shouldn't it? Feb 27, 2017 at 9:14
  • 1
    yes, it is possible: try removing nodes near coords from your axis, and then add something like : \addplot[only marks, nodes near coords, point meta=explicit symbolic] coordinates { (1930,50e6) [ -24 ] (1940,42e6) [ +27 ] (1950,43e6) [ +7.5 ] (1960,50e6) [ -10 ] (1970,70e6) [ -7 ] };
    – Thruston
    Feb 27, 2017 at 9:28

2 Answers 2

3

You can also have pgfplots calculate the difference for you:

\documentclass{article}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.14}

\pgfplotstableread{
x y1 y2
1930 50e6 38e6
1940 33e6 42e6
1950 40e6 43e6
1960 50e6 45e6
1970 70e6 65e6
}\mydata

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ymin=0,
    x tick label style={
        /pgf/number format/1000 sep=},
    ylabel=Population,
    enlarge x limits=0.15,
    legend style={at={(0.5,-0.15)},
        anchor=north,legend columns=-1},
    ybar=5pt,% configures `bar shift'
    bar width=9pt,
]
\addplot table[x=x,y=y1] {\mydata};
\addplot table[x=x,y=y2] {\mydata};

\addplot [
  only marks,
  point meta=explicit,
  nodes near coords={\pgfmathprintnumber[precision=1,showpos]{\pgfplotspointmeta}\%},
  nodes near coords style={above}
  ]
  table[
    x=x,
    y expr={max(\thisrow{y1},\thisrow{y2})},
    meta expr={(\thisrow{y2}-\thisrow{y1})/\thisrow{y1} * 100}] {\mydata};

\legend{Far,Near}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

5

Try this:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    x tick label style={
        /pgf/number format/1000 sep=},
    ylabel=Population,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.15)},
        anchor=north,legend columns=-1},
    ybar=5pt,% configures `bar shift'
    bar width=9pt,
    %nodes near coords,
    point meta=y *10^-7 % the displayed number
]
\addplot coordinates {(1930,50e6) (1940,33e6) (1950,40e6) (1960,50e6) (1970,70e6)}; 
\addplot coordinates {(1930,38e6) (1940,42e6) (1950,43e6) (1960,45e6) (1970,65e6)};
\addplot[only marks, nodes near coords, point meta=explicit symbolic]
    coordinates { (1930,50e6) [ $-24$ ] (1940,42e6) [ $+27$ ] (1950,43e6) [ $+7.5$ ]
    (1960,50e6) [ $-10$ ] (1970,70e6) [ $-7$ ] }; 

\legend{Far,Near}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

4
  • Was just about to comment that I compiled on my machine and it worked. Thanks! I also checked that I could change the numbers to percents and they remained nicely centered. Feb 27, 2017 at 9:32
  • 1
    Notice that to get proper minus signs you need to put the labels in math mode with $...$.
    – Thruston
    Feb 27, 2017 at 9:34
  • Do I need math mode for percents? Feb 27, 2017 at 9:35
  • You can put a % in math mode or text mode - they look the same as far as I can tell. Obviously you need to escape it in either mode to stop it being a comment.
    – Thruston
    Feb 27, 2017 at 9:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .