4

I'm trying to find a way to present statistical data, specifically percentages that add up to 100. I'm not a fan of pie charts and I've found something I like using stacked xbars.

This is what I have at the moment

    \documentclass{article} 
    \usepackage{pgfplots}
    \usepackage{tikz}

    \begin{document}
    \pgfplotsset{testbar/.style={
            title=Some title,
            xbar stacked,
            width=10cm,
            axis y line*= none, axis x line*= none,
            xmajorgrids = false,
            xmin=0,xmax=100,
            ytick = data,
            yticklabels = {Some label},
            tick align = outside, xtick pos = left,
            bar width=8mm, y=10mm,
            enlarge y limits={abs=0.625},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
            nodes near coords,
            nodes near coords align={left},
            point meta=x,
        }}
    \begin{tikzpicture}
    \begin{axis}[testbar] 
     \addplot[fill=blue!40] coordinates{(46.25,0) };
     \addplot[fill=red!40] coordinates{(53.75,0)};
    \end{axis}
    \end{tikzpicture}
    \end{document}plot[fill=red!40] coordinates{(53.75,0)};
    \end{axis}
    \end{tikzpicture}
    \end{document}

Current output

As you can see, the label for the second (red) plot is incorrect, I'd like it to be 53.75. The piece of code that changes it is "point meta=x", which I believe marks the ending point of the plot, but this isn't adequate for the second plot. How can I fix this?

Thank you for your help, and I apologize if my question is trivial. This is completely new to me and I wasn't able to find any other questions on the site to resolve these particular details.

  • 1
    Welcome to TeX.SX! Please make your example compilable (MWE). – TeXnician May 14 '17 at 17:49
  • Please ask only a single question in a thread. – esdd May 14 '17 at 18:29
  • Added MWE, cut it down to one question. – James May 14 '17 at 21:02
  • 2
    You can change point meta=x to point meta=rawx to get the desired value. – Jake May 14 '17 at 21:28
4

When you load pgfplots then set immadetly a value for compat. If this is missing pgfplots uses compat=pre 1.3 to ensure the compatibility to this really old version.

With at least compat=1.9 you only have to remove nodes near coords align={left}, point meta=x to get

enter image description here

\documentclass{article} 
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{
  compat=1.14,% <- current version is 1.14
}

\begin{document}
\pgfplotsset{testbar/.style={
        title=Some title,
        xbar stacked,
        width=10cm,
        axis lines*= left,
        xmin=0,xmax=100,
        ytick = data,
        yticklabels = {Some label},
        tick align = outside,
        bar width=8mm,
        y=10mm,
        enlarge y limits={abs=0.6},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
        nodes near coords
    }}
\begin{tikzpicture}
\begin{axis}[testbar] 
 \addplot[fill=blue!40] coordinates{(46.25,0) };
 \addplot[fill=red!40] coordinates{(53.75,0)};
\end{axis}
\end{tikzpicture}
\end{document}plot[fill=red!40] coordinates{(53.75,0)};
\end{axis}
\end{tikzpicture}
\end{document}

If you really like the old position of the nodes near coords, then change the compatibility settings only for the bar nodes to 1.8 using compat/bar nodes=1.8 and use nodes near coords align={left},point meta=rawx:

enter image description here

Code:

\documentclass{article} 
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{
  compat=1.14,% <- added, current version is 1.14
  compat/bar nodes=1.8% <- to use the old settings for bar nodes
}

\begin{document}
\pgfplotsset{testbar/.style={
        title=Some title,
        xbar stacked,
        width=10cm,
        axis lines*= left,
        xmin=0,xmax=100,
        ytick = data,
        yticklabels = {Some label},
        tick align = outside,
        bar width=8mm,
        y=10mm,
        enlarge y limits={abs=0.6},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
        nodes near coords,
        nodes near coords align={left},
        point meta=rawx
    }}
\begin{tikzpicture}
\begin{axis}[testbar] 
 \addplot[fill=blue!40] coordinates{(46.25,0) };
 \addplot[fill=red!40] coordinates{(53.75,0)};
\end{axis}
\end{tikzpicture}
\end{document}plot[fill=red!40] coordinates{(53.75,0)};
\end{axis}
\end{tikzpicture}
\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.