5

I try to create a bar graph based on an example I found.

I do some modification to origin, but it doesn't work.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[title  = Contributions per category
                          at LaTeX-Community.org,
    xbar,
    y axis line style = { opacity = 0 },
    axis x line       = none,
    tickwidth         = 0pt,
    enlarge y limits  = 0.2,
    enlarge x limits  = 0.02,
    symbolic y coords = {specrand, sphinx3, mcf, gcc, Distributions, Editors},
    nodes near coords,
  ]
  \addplot coordinates { (57727,specrand) (123, sphinx3) (2222,mcf)        (5672,gcc)
                         (2193,Distributions)  (11106,Editors) };
  \legend{Topics, Posts}
  \end{axis}
\end{tikzpicture}
\end{document}

Error log

Package pgfplots Error: Sorry, the input coordinate ` sphinx3' h
as not been defined with 'symbolic y coords={specrand, sphinx3, mcf, gcc, Distr
ibutions, Editors}... Maybe it has been misspelled? Or did you mean something l
ike [normalized] sphinx3?.

See the pgfplots package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.20 ...  (2193,Distributions)  (11106,Editors) };

What's wrong with LaTeX syntax? Thanks.

9

Spaces. If you look carefully at the error message you see it refers to the symbolic coordinate called ' sphinx3', i.e. with a space first. In the list of symbolic y coords leading spaces are stripped, but this does not happen in the coordinate list for \addplot. So all you have to do is to change from (132, sphinx3) to (132,sphinx3).

You might also want to add ytick=data to the axis options.

To add a unit after the numbers, redefine nodes near coords. The default value is \pgfmathprintnumber\pgfplotspointmeta, so if you want a percentage sign after the number, use nodes near coords ={\pgfmathprintnumber\pgfplotspointmeta\%}.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[title  = Contributions per category
                          at LaTeX-Community.org,
    xbar,
    y axis line style = { opacity = 0 },
    ytick             = data,
    typeset ticklabels with strut, % improves vertical alignment of some of the ticklabels in this case (compare gcc and mcf with and without)
    axis x line       = none,
    tickwidth         = 0pt,
    enlarge y limits  = 0.2,
    enlarge x limits  = 0.02,
    symbolic y coords = {specrand, sphinx3, mcf, gcc, Distributions, Editors},
    nodes near coords ={\pgfmathprintnumber\pgfplotspointmeta\%},
  ]
  \addplot coordinates { (57727,specrand) (123,sphinx3) (2222,mcf) (5672,gcc)
                         (2193,Distributions)  (11106,Editors) };
  \legend{Topics, Posts}
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you. An irrelevant question: how to set unit for data? – KyL Apr 29 '16 at 7:46
  • 1
    I'd get rid of the darker border around the bars, Tufte-style ;-) – Astrinus Apr 29 '16 at 7:55
  • @KyL Where do you want the unit printed? – Torbjørn T. Apr 29 '16 at 8:16
  • @TorbjørnT. percentage (%) – KyL Apr 29 '16 at 10:39
  • @KyL That's not what I asked, I meant where in the plot do you want to have the unit? And 57000% Do you want the percent of the sum of all the numbers, so there is some calculations involved? Or are you talking about something completely different? If that is the case, then I would suggest making a new question. – Torbjørn T. Apr 29 '16 at 10:48

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.