1

I'm trying to create a bar chart with a lot of information and the final result I get is really messy. Is there any way to make it look something like in the picture? I don't want to use one image from excel as my other graphs are made in latex enter image description here

\documentclass[12pt,a4paper,oneside]{report}
\usepackage{pgfplots,filecontents} 
\pgfplotsset{width=14cm,compat=1.9}
\pgfplotstableread[row sep=\\,col sep=&]{
    Category&2007&2008&2009&2010&2011&2012&2013&2014&2015 \\
        CS  & 29 & 22   & 32   & 30   & 27  & 19 & 22 & 11 & 13 \\
        CRO & 20 & 11   & 14   & 23   & 17  & 14 & 9  & 9  & 8 \\
        NCO & 23 & 20   & 28   & 35   & 35  & 19 & 24 & 14 & 19 \\
        Ph  & 13 & 23   & 10   & 16   & 14  & 14 & 7  & 10 & 10 \\
        IS  & 52 & 32   & 38   & 34   & 40  & 28 & 34 & 22 & 29 \\
        L   & 8  & 21   & 11   & 14   & 6   & 6  & 10 & 10 & 8 \\
        NUK & 4  & 7    & 9    & 9    & 10  & 18 & 19 & 19 & 15\\
}\mydata


\begin{document}


\begin{tikzpicture}
\begin{axis}[
ybar,
symbolic x coords={CS,CRO,NCO,Ph,IS,L,NUK},
xtick=data,
]
\addplot table[x=Category,y=2007]{\mydata};
\addplot table[x=Category,y=2008]{\mydata};
\addplot table[x=Category,y=2009]{\mydata};
\addplot table[x=Category,y=2010]{\mydata};
\addplot table[x=Category,y=2011]{\mydata};
\addplot table[x=Category,y=2012]{\mydata};
\addplot table[x=Category,y=2013]{\mydata};
\addplot table[x=Category,y=2014]{\mydata};
\addplot table[x=Category,y=2015]{\mydata};

\end{axis}
\end{tikzpicture}


\end{document}
  • Is bar width=3pt enough? – Torbjørn T. Aug 29 '17 at 20:08
  • I think so, but now i'm getting this error Package PGF Math Error: Unknown operator s' or s@' (in '4pts'). \end{axis} – user141098 Aug 29 '17 at 20:34
  • Looks like you wrote 4pts instead of 4pt. – Torbjørn T. Aug 29 '17 at 20:45
1

See how you think this looks. I used a colormap from the colorbrewer library. There are some comments in the code, but ask if anything is unclear.

output of code

\documentclass[12pt,a4paper,oneside]{report}
\usepackage{pgfplots} 
\usepgfplotslibrary{colorbrewer}
\pgfplotsset{width=14cm,compat=1.9}
\pgfplotstableread[row sep=\\,col sep=&]{
    Category&2007&2008&2009&2010&2011&2012&2013&2014&2015 \\
        CS  & 29 & 22   & 32   & 30   & 27  & 19 & 22 & 11 & 13 \\
        CRO & 20 & 11   & 14   & 23   & 17  & 14 & 9  & 9  & 8 \\
        NCO & 23 & 20   & 28   & 35   & 35  & 19 & 24 & 14 & 19 \\
        Ph  & 13 & 23   & 10   & 16   & 14  & 14 & 7  & 10 & 10 \\
        IS  & 52 & 32   & 38   & 34   & 40  & 28 & 34 & 22 & 29 \\
        L   & 8  & 21   & 11   & 14   & 6   & 6  & 10 & 10 & 8 \\
        NUK & 4  & 7    & 9    & 9    & 10  & 18 & 19 & 19 & 15\\
}\mydata


\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
symbolic x coords={CS,CRO,NCO,Ph,IS,L,NUK},
xtick=data,
% reduce height of axis a bit
height=9cm,
% set width of bars
bar width=3pt,
% remove gap below bars
ymin=0,
% remove frame around legend, add some space
legend style={draw=none,column sep=2mm},
legend columns=2,
% customize how the legend images are drawn
% draw a square instead of two bars
legend image code/.code={%
       \draw[#1,draw=none,/tikz/.cd,yshift=-0.25em]
        (0cm,1pt) rectangle (6pt,7pt);},
% color map from colorbrewer
cycle list/Paired,
% the above only sets the color, need to specify that bars should be filled
every axis plot/.append style={fill}
]
\addplot table[x=Category,y=2007]{\mydata};
\addplot table[x=Category,y=2008]{\mydata};
\addplot table[x=Category,y=2009]{\mydata};
\addplot table[x=Category,y=2010]{\mydata};
\addplot table[x=Category,y=2011]{\mydata};
\addplot table[x=Category,y=2012]{\mydata};
\addplot table[x=Category,y=2013]{\mydata};
\addplot table[x=Category,y=2014]{\mydata};
\addplot table[x=Category,y=2015]{\mydata};

\legend{2007,2008,2009,2010,2011,2012,2013,2014,2015}
\end{axis}
\end{tikzpicture}
\end{document}

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.