![pgfplots,条形图,轴距](https://linux22.com/image/280151/pgfplots%EF%BC%8C%E6%9D%A1%E5%BD%A2%E5%9B%BE%EF%BC%8C%E8%BD%B4%E8%B7%9D.png)
我有一张以下图表,我正在尝试减小 x 轴刻度 FCC 和 Beta pi 之间的距离。有什么建议可以减少这个距离吗?
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
%legend columns=-1,
legend cell align=left,
every axis plot post/.style={/pgf/number format/fixed},
ybar=2pt,
bar width=10pt,
x=5cm,
y=4.5cm,
ymin=0,
axis on top,
%ymax=12,
xtick=data,
%xlabel=Cores,
ylabel=Peak Energy (E),
enlarge x limits=0.2,
%enlarge y limits={abs=0.5},
symbolic x coords={FCC,Beta pi},
%restrict y to domain*=0:11, % Cut values off at 14
visualization depends on=rawy\as\rawy, % Save the unclipped values
after end axis/.code={ % Draw line indicating break
\draw [ultra thick, white] (rel axis cs:0,1.05) -- (rel axis cs:1,1.05);
},
nodes near coords={\scriptsize{\pgfmathprintnumber{\rawy}}
},
axis lines*=left,
clip=false,
area legend
%legend style={at={(0.6,0.8)},anchor=west}
]
\addplot[fill=red!40] coordinates {(FCC,0.7) (Beta pi,0.66) };
\addplot[fill=yellow!40] coordinates {(FCC,1) (Beta pi,0.81) };
\addplot[fill=blue!40] coordinates {(FCC,0.6) (Beta pi,0.74)};
\addplot[fill=green!40] coordinates {(FCC,0.5) (Beta pi,0.7)};
\legend{x264,bodytrack,swaptions,blacksholes};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
有不同的方法可以消除那个大块空白:
- 减小图表的整体宽度,使条形图组更加紧密。您可以通过为 x 轴指定单位向量来实现此目的。如果您添加更多数据,图表将自动增大。
- 保持图表的整体宽度,但将条形图组移得更近。您可以通过设置更大的值来实现这一点。您可以使用序言中的“如果有”或“更新
enlarge x limits
”,以轴单位设置此值。abs=1
\pgfplotsset{compat=1.8}
- 增加条形的宽度,以填充条形组之间的空白。您可以使用 键执行此操作
bar width
。请注意,您还必须增加值enlarge x limits
,并且您可能希望通过设置 来删除条形组内的空间ybar=0pt
。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\parbox{14cm}{
\pgfmathsetseed{2}
{\Large \texttt{ybar}}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0,
samples=2,
domain=1:2,
xtick=data
]
\addplot {rnd};
\addplot {rnd};
\end{axis}
\end{tikzpicture}\\
{\Large \texttt{ybar, x=1cm, enlarge x limits=\{abs=0.5cm\}}}
\pgfmathsetseed{2}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0,
samples=2,
domain=1:2,
xtick=data,
x=1cm,
enlarge x limits={abs=0.5cm}
]
\addplot {rnd};
\addplot {rnd};
\end{axis}
\end{tikzpicture}\\
{\Large \texttt{ybar, enlarge x limits=\{abs=1\}}}
\pgfmathsetseed{2}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0,
samples=2,
domain=1:2,
xtick=data,
enlarge x limits={abs=1}
]
\addplot {rnd};
\addplot {rnd};
\end{axis}
\end{tikzpicture}\\
{\Large \texttt{ybar=0pt, bar width=0.45, enlarge x limits=\{abs=0.5\}}}
\pgfmathsetseed{2}
\pgfmathsetseed{2}
\begin{tikzpicture}
\begin{axis}[
ybar=0pt,
ymin=0,
samples=2,
domain=1:2,
xtick=data,
bar width=0.45,
enlarge x limits={abs=0.5}
]
\addplot {rnd};
\addplot {rnd};
\end{axis}
\end{tikzpicture}
}
\end{document}