我已经用 pgfplots 制作了我的第一个条形图,它看起来像这样:
如您所见,第一个和最后一个条形图并不完全可见,当我尝试添加另一个图时,情况变得更糟。如何在 y 轴和这些条形图之间插入水平空间,或者通过其他方式使这些条形图完全可见?
该图表由 pdflatex 生成,使用如下命令:
\begin{tikzpicture}
\begin{axis}
[
symbolic x coords={36,72,144,288,576},
xtick=data,
ylabel=Time in Seconds,
every axis y label/.style={at={(ticklabel cs:0.5)},rotate=90,anchor=near ticklabel},
xlabel=Memory in MB,
every axis x label/.style={at={(ticklabel cs:0.5)},anchor=near ticklabel},
legend style={at={(0.5,-0.25)},
anchor=north,legend columns=-1},
ybar,
bar width=7pt,
]
\addplot
coordinates {(36,1051.6) (72,875.0) (144,1099.3) (288,1114.6) (576,1054.3)};
\addplot
coordinates {(36,797.3) (72,717.6) (144,608.3) (288,591.6) (576,544.3)};
\addplot
coordinates {(36,735.3) (72,590.3) (144,533.6) (288,500.6) (576,447.0)};
\addplot
coordinates {(36,685.3) (72,544.0) (144,477.0) (288,451.6)(576,430.0)};
\legend{\,1 CPU\hspace*{0.5cm},\,2 CPUs\hspace*{0.5cm},\,3 CPUs\hspace*{0.5cm},\,4 CPUs}
\end{axis}
\end{tikzpicture}
答案1
您可以使用键enlarge x limits=<factor>
在侧面添加额外的空间。enlarge x limits=0.15
结合起来ybar=0.6pt
效果很好:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
symbolic x coords={36,72,144,288,576},
xtick=data,
ylabel=Time in Seconds,
every axis y label/.style={at={(ticklabel cs:0.5)},rotate=90,anchor=near ticklabel},
xlabel=Memory in MB,
every axis x label/.style={at={(ticklabel cs:0.5)},anchor=near ticklabel},
legend style={at={(0.5,-0.25)},
anchor=north,legend columns=-1},
ybar,
bar width=6pt,
enlarge x limits=0.15
]
\addplot
coordinates {(36,1051.6) (72,875.0) (144,1099.3) (288,1114.6) (576,1054.3)};
\addplot
coordinates {(36,797.3) (72,717.6) (144,608.3) (288,591.6) (576,544.3)};
\addplot
coordinates {(36,735.3) (72,590.3) (144,533.6) (288,500.6) (576,447.0)};
\addplot
coordinates {(36,685.3) (72,544.0) (144,477.0) (288,451.6)(576,430.0)};
\legend{\,1 CPU\hspace*{0.5cm},\,2 CPUs\hspace*{0.5cm},\,3 CPUs\hspace*{0.5cm},\,4 CPUs}
\end{axis}
\end{tikzpicture}
\end{document}