我想制作一个有九个条形图的图表。如您所见,它们在代码中为 (0-8)。不幸的是,它只能制作八个条形图。
\documentclass[12pt]{report}
\usepackage{polski}
\usepackage{pgfplots,tikz}
\pagestyle{empty}
\pgfplotsset{width=14cm,compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
x tick label style={
/pgf/number format/1000 sep=},
ylabel=Prawdopodobieństwo,
enlargelimits=0.02,
legend style={at={(0.5,-0.15)},
anchor=north,legend=-1},
ybar interval=0.8,
]
\addplot
coordinates{
(0,0.00390625)
(1,0.03125)
(2,0.109375)
(3,0.21875)
(4,0.273438)
(5,0.21875)
(6,0.109375)
(7,0.03125)
(8,0.00390625)
};
\addplot
coordinates{
(0,0.00405)
(1,0.03143)
(2,0.10847)
(3,0.21984)
(4,0.2723)
(5,0.2166)
(6,0.11272)
(7,0.03062)
(8,0.00397)
};
\addplot
coordinates{
(0,0.00261035)
(1,0.0237304)
(2,0.107973)
(3,0.221879)
(4,0.270052)
(5,0.22449)
(6,0.108685)
(7,0.0363075)
(8,0.00427148)
};
\legend{Rozklad teoretyczny,Rozklad dla n=100000,Rozklad dla n=4214}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
ybar interval=0.8
如果我将其更改为 justybar
并删除,这对我来说是有效的enlargelimits=0.02
。我想你可能需要从工作图开始,更仔细地调整设置,并朝着你想要的方向前进。
答案2
如果要使用该ybar interval
样式,则需要为每个图提供一个额外的“虚拟数据点”。这是必要的,因为间隔样式用于绘制范围内的条形图,而不是点,因此为了计算最后一个范围的宽度,您需要提供一个上限。
\documentclass[12pt]{report}
\usepackage{polski}
\usepackage{pgfplots,tikz}
\pagestyle{empty}
\pgfplotsset{width=14cm,compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar interval,
x tick label style={
/pgf/number format/1000 sep=},
ylabel=Prawdopodobieństwo,
enlargelimits=0.02,
legend style={at={(0.5,-0.15)},
anchor=north,legend=-1},
]
\addplot
coordinates{
(0,0.00390625)
(1,0.03125)
(2,0.109375)
(3,0.21875)
(4,0.273438)
(5,0.21875)
(6,0.109375)
(7,0.03125)
(8,0.00390625)
(9,0)
};
\addplot
coordinates{
(0,0.00405)
(1,0.03143)
(2,0.10847)
(3,0.21984)
(4,0.2723)
(5,0.2166)
(6,0.11272)
(7,0.03062)
(8,0.00397)
(9,0)
};
\addplot
coordinates{
(0,0.00261035)
(1,0.0237304)
(2,0.107973)
(3,0.221879)
(4,0.270052)
(5,0.22449)
(6,0.108685)
(7,0.0363075)
(8,0.00427148)
(9,0)
};
\legend{Rozklad teoretyczny,Rozklad dla n=100000,Rozklad dla n=4214}
\end{axis}
\end{tikzpicture}
\end{document}
或者,您也可以直接使用ybar
样式。在这种情况下,您应该使用enlarge y limits=0.02
而不是 ,enlargelimits=0.02
以确保没有条形图被从侧面切断。请特别注意结果图中刻度线和垂直分割线之间的差异。