2

I am trying out tikz package to create bar diagrams and following the code example in the manual on page 82 http://ftp.fernuni-hagen.de/ftp-dir/pub/mirrors/www.ctan.org/graphics/pgf/contrib/pgfplots/doc/pgfplots.pdf#page=82

gives me a broken bar diagram.

\documentclass{scrreprt}

\usepackage{pgfplots}

\begin{tikzpicture}
 \begin{axis}
 [
  ybar,
  enlargelimits=0.15,
  legend style={at={(0.5,-0.15)},
  anchor=north,legend columns=-1},
  ylabel={Anzahl der täglich hochgeladenen Fotos (in Millionen)},
  symbolic x coords={2008,2009,2010,2011,2012,2013,2014},
  xtick=data,
  nodes near coords,
  nodes near coords align={vertical},
 ]
 \addplot coordinates {(2008,17) (2008,0) (2008,0) (2008,0)};
 \addplot coordinates {(2009,69) (2009,0) (2009,0) (2009,0)};
 \addplot coordinates {(2010,197) (2010,0) (2010,0) (2010,0)};
 \addplot coordinates {(2011,249) (2011,9) (2011,0) (2011,0)};
 \addplot coordinates {(2012,300) (2012,9) (2012,51) (2012,0)};
 \addplot coordinates {(2013,343) (2013,51) (2013,394) (2013,394)};
 \addplot coordinates {(2014,351) (2014,60) (2014,703) (2014,703)};
\legend{Facebook,Instagram,Snapchat,WhatsApp}
\end{axis}
\end{tikzpicture}

the concrete problems are:

  • the bars are not shown correctly

  • the values are either out of bound or mixed together

  • the bar diagram is generally too small (how do I specify \linewidth?)

  • Why are you insert a lot of equal coordinates: (2009,69) (2009,0) (2009,0) (2009,0)? The last 3 are equal. – Sigur Jun 2 '15 at 19:16
  • my symbolic x coords are years so if I did not misunderstood the manual I have to write the y coords for each corresponding x coord. in that case some data where not available for that year thus 0 – sceiler Jun 2 '15 at 19:21
4

Note, that the first \addplotplot draws the graph for the first label, i.e. your first \addplotcomment should contain all Facebook coordinates. So I think you have to change the assignment of the coordinates to the \addplot commands. Additionally I change the width of the axis, the bar width and enlargelimits and set the compat to the current version 3.12.

enter image description here

Code:

\documentclass{scrreprt}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}% current version

\begin{document}
\noindent\begin{tikzpicture}
  \begin{axis}[
    width=\linewidth,
    ybar,
    bar width=8pt,
    ymin=0,
    enlarge x limits={abs=25pt},
    legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-1},
    ylabel={Anzahl der täglich hochgeladenen Fotos (in Millionen)},
    symbolic x coords={2008,2009,2010,2011,2012,2013,2014},
    xtick=data,
    nodes near coords,
    every node near coord/.append style={rotate=90, anchor=west,font=\footnotesize},
    nodes near coords align={vertical},
  ]
  \addplot coordinates {% Facebook
    (2008,17) (2009,69) (2010,197) (2011,249) (2012,300) (2013,343) (2014,351)};
  \addplot coordinates {% Instagram
    (2008,0) (2009,0) (2010,0) (2011,9) (2012,9) (2013,51) (2014,60)};
  \addplot coordinates {% Snapshot
    (2008,0) (2009,0) (2010,0) (2011,0) (2012,51) (2013,394) (2014,703)};
  \addplot coordinates {% WhatsApp
    (2008,0) (2009,0) (2010,0) (2011,0) (2012,0) (2013,394) (2014,703)};
  \legend{Facebook,Instagram,Snapchat,WhatsApp}
\end{axis}
\end{tikzpicture}
\end{document}

Update: Of course you can also a text file like

Year, Facebook, Instagram, Snapchat, WhatsApp
2008, 17, 0,0,0 
2009,69,0,0,0
2010,197,0,0,0
2011,249,9,0,0
2012,300,9,51,0
2013,343,51,394,394
2014,351,60,703,703

Then you have to use

\addplot table[col sep=comma,x=Year,y=Facebook]{<filename>};
\addplot table[col sep=comma,x=Year,y=Instagram]{<filename>};
\addplot table[col sep=comma,x=Year,y=Snapchat]{<filename>};
\addplot table[col sep=comma,x=Year,y=WhatsApp]{<filename>};

Or you use \pgfplotsinvokeforeach to plot more than one column from a file:

\begin{filecontents*}{data\jobname.csv}
Year, Facebook, Instagram, Snapchat, WhatsApp
2008, 17, 0,0,0 
2009,69,0,0,0
2010,197,0,0,0
2011,249,9,0,0
2012,300,9,51,0
2013,343,51,394,394
2014,351,60,703,703
\end{filecontents*}

\documentclass{scrreprt}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}% current version

\begin{document}
\noindent\begin{tikzpicture}
  \begin{axis}[
    width=\linewidth,
    ybar,
    bar width=8pt,
    ymin=0,
    enlarge x limits={abs=25pt},
    legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-1},
    ylabel={Anzahl der täglich hochgeladenen Fotos (in Millionen)},
    symbolic x coords={2008,2009,2010,2011,2012,2013,2014},
    xtick=data,
    nodes near coords,
    every node near coord/.append style={rotate=90, anchor=west,font=\footnotesize},
    nodes near coords align={vertical},
  ]
  \pgfplotsinvokeforeach{Facebook,Instagram,Snapchat,WhatsApp}{
    \addplot table[col sep=comma,x=Year,y=#1]{data\jobname.csv};
    \addlegendentry{#1}
  }
\end{axis}
\end{tikzpicture}
\end{document}

The result is the same as above.

| improve this answer | |
  • workfs perfect. thanks. One additional thing.. how do I specify the usage of a csv file where the date is in this format: Year, Facebook, Intagram, Snapchat, Whatsapp 2008, 17, 0,0,0 2009, 123,31,123,123 and so on. I get this \addplot table[x=Jahr, y=Summe] {data2.csv}; command but I "more" x and y values. is there a way? – sceiler Jun 3 '15 at 11:44
  • 1
    See my updated answer. – esdd Jun 3 '15 at 12:52
  • sorry but just one final question then I am satisfied. :) I defined these colors: \definecolor{blue}{RGB}{0,150,214} \definecolor{greenaccent}{RGB}{0,139,43} \definecolor{purpleaccent}{RGB}{130,41,128} \definecolor{orangeaccent}{RGB}{240,83,50} how do I get them into my graph? facebook would be blue, instagram green and so on. \addplot[fill=blue] colors all bars, so I would need something like: y#1fill=blue – sceiler Jun 3 '15 at 13:37

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.