我正在尝试创建一个包含大量信息的条形图,但最终得到的结果非常混乱。有没有办法让它看起来像图片中的那样?我不想使用 excel 中的一张图片,因为我的其他图表都是用 latex 制作的
\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}
答案1
看看你觉得这个看起来怎么样。我使用了库中的色图colorbrewer
。代码中有一些注释,但如果有任何不清楚的地方,请询问。
\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}