我正在尝试使用 tikz 包来创建条形图,并按照手册第 84 页中的代码示例进行操作http://mirrors.ctan.org/graphics/pgf/contrib/pgfplots/doc/pgfplots.pdf#page=84
给我一个断裂的条形图。
\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}
具体问题是:
条形图显示不正确
值超出范围或混合在一起
条形图通常太小(我如何指定
\linewidth
?)
答案1
请注意,第一个\addplotplot
绘制第一个标签的图形,即您的第一条\addplot
评论应包含所有 Facebook 坐标。因此,我认为您必须更改命令的分配coordinates
。\addplot
此外,我更改了width
、axis
和bar width
并将enlargelimits
设置compat
为当前版本 3.12。
代码:
\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}
更新:当然你也可以使用文本文件
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
然后你必须使用
\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>};
或者您可以使用\pgfplotsinvokeforeach
从文件中绘制多个列:
\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}
结果和上面一样。