\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{testbar/.style={
title=strookdiagram sporten,
xbar stacked,
width=10cm,
axis y line*= none, axis x line*= bottom,
xmajorgrids = true,
xmin=0,xmax=100,
ytick = data,
yticklabels = {},
tick align = outside, xtick pos = left,
bar width=6mm, y=8mm,
enlarge y limits={abs=0.625},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
nodes near coords,
nodes near coords align={left},
}}
\begin{tikzpicture}
\begin{axis}[testbar]
\addplot[fill=red!40] coordinates{(20,0) };
\addplot[fill=gray!40] coordinates{(10,0)};
\addplot[fill=green!40] coordinates{(20,0) };
\addplot[fill=orange!40] coordinates{(40,0) };
\addplot[fill=blue!40] coordinates{(3,0) };
\addplot[fill=red!40] coordinates{(7,0) };
\end{axis}
\end{tikzpicture}
\end{document}
我想制作一个水平堆叠条形图,带有图例和每部分的百分比。上面的代码给了我:
但如您所见,百分比均为零。我还想要一个图例。提前致谢。
编辑
我在 Jesse 的帮助下更改了代码,所以我现在的代码是:
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\begin{document}
\pgfplotsset{testbar/.style={
title=strookdiagram sporten,
title style={yshift=10pt},
xbar stacked, area style,
width=10cm,
axis y line*= none, axis x line*= bottom,
xmajorgrids = true,
xmin=0,
xmax=100,
x=1mm,
xtick={0,10,...,100},
ytick = data,
yticklabels = {},
tick align = outside, xtick pos = left,
bar width=6mm, y=8mm,
enlarge y limits={abs=0.625},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
%nodes near coords,
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
every node near coord/.append style={rotate=-90,font=\small, left,shift={(axis direction cs:-3,0)}}
}}
\begin{tikzpicture}
\begin{axis}[testbar,legend style={area legend, at={(0.5,-0.85)}, anchor=north, legend columns=-1}]
\addplot[fill=red!40] coordinates{(20,0) };
\addplot[fill=gray!40] coordinates{(10,0)};
\addplot[fill=green!40] coordinates{(20,0) };
\addplot[fill=orange!40] coordinates{(40,0)};
\addplot[fill=blue!40] coordinates{(3,0) };
\addplot[fill=red!40] coordinates{(7,0) };
\legend{Voetbal,Netbal,C,D,E,F}
\end{axis}
\end{tikzpicture}
\end{document}
但它给我的印象是现在
我已经使用 TeX Live Utilty 更新了所有软件包,我的 TeX 编辑器是 TeXpad。
答案1
这次尝试尝试提供两种解决方案。由于堆叠意味着将连续\addplot
命令的 x 或 y 坐标添加到彼此之上(此处为 x 方向),因此特别会导致 90% 和 3% 的重叠。
上面的第一个解决方案是将标签旋转 -90 度并使用小字体大小。要做到这一点,需要通过every node near coord/.append style
命令定义附加样式。需要注意的是,shift 命令shift={(axis direction cs:-3,0)}
是将标签向上移动,这样它们就不会与框发生冲突,从而提高可读性。
底部的第二个图只是将 y 坐标从相同的 y=0 更改为 y=0,1,2,3,4,5,从而使它们像楼梯一样,而不改变默认的标签方向。此外,在此图中,您将观察到如果标签折叠为单个水平堆叠条,它们为什么会重叠。
至于图例,图例样式是通过 定义legend style={area legend, at={(0.5,-0.15)}, anchor=north, legend columns=-1}
。
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\begin{document}
\pgfplotsset{testbar/.style={
title=strookdiagram sporten,
xbar stacked, area style,
width=10cm,
axis y line*= none, axis x line*= bottom,
xmajorgrids = true,
xmin=0,xmax=100,
ytick = data,
yticklabels = {},
tick align = outside, xtick pos = left,
bar width=6mm, y=8mm,
enlarge y limits={abs=0.625},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
nodes near coords,
every node near coord/.append style={rotate=-90,font=\small, left,shift={(axis direction cs:-3,0)}}
}}
\begin{tikzpicture}
\begin{axis}[testbar,legend style={area legend, at={(0.5,-0.85)}, anchor=north, legend columns=-1}]
\addplot[fill=red!40] coordinates{(20,0) };
\addplot[fill=gray!40] coordinates{(10,0)};
\addplot[fill=green!40] coordinates{(20,0) };
\addplot[fill=orange!40] coordinates{(40,0)};
\addplot[fill=blue!40] coordinates{(3,0) };
\addplot[fill=red!40] coordinates{(7,0) };
\legend{A,B,C,D,E,F}
\end{axis}
\end{tikzpicture}
\vspace{1cm}
\pgfplotsset{testbar/.style={
title=strookdiagram sporten,
xbar stacked, area style,
width=10cm,
axis y line*= none, axis x line*= bottom,
xmajorgrids = true,
xmin=0,xmax=100,
ytick = data,
yticklabels = {},
tick align = outside, xtick pos = left,
bar width=6mm, y=8mm,
enlarge y limits={abs=0.625},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
nodes near coords,
nodes near coords align={left}
}}
\begin{tikzpicture}
\begin{axis}[testbar,legend style={area legend, at={(0.5,-0.15)}, anchor=north, legend columns=-1}]
\addplot[fill=red!40] coordinates{(20,0)};
\addplot[fill=gray!40] coordinates{(10,1)};
\addplot[fill=green!40] coordinates{(20,2) };
\addplot[fill=orange!40] coordinates{(40,3) };
\addplot[fill=blue!40] coordinates{(3,4) };
\addplot[fill=red!40] coordinates{(7,5) };
\legend{A,B,C,D,E,F}
\end{axis}
\end{tikzpicture}
\end{document}
编辑:
楼主喜欢用百分比代替数字。然后把相应的行替换成
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
。要将标题进一步上移,请将其添加
title style={yshift=10pt}
到第一个图的轴,因为添加的 % 将与旧设置中的标题发生冲突。要删除顶部的网格,请添加
axis on top=false
到testbar/.style
进行这些更改后,新的输出如下所示。