\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}
我想製作一個水平堆疊條形圖,帶有圖例和每個部分的百分比。上面的程式碼給了我:
但正如您所看到的,百分比全部為零。我也想要一個傳奇。提前致謝。
編輯
我在傑西的幫助下更改了程式碼,所以現在我的程式碼是:
\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
本次嘗試試圖提供兩種解決方案。由於堆疊意味著將連續命令的 x 或 y 座標添加\addplot
到彼此的頂部(此處為 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}
編輯:
OP 喜歡使用百分比而不是數字。然後將對應的行替換為
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
.若要將標題進一步向上移動,請新增
title style={yshift=10pt}
到第一個圖的軸,因為新增的 % 會與舊設定中的標題發生衝突。若要刪除頂部的網格,請新增
axis on top=false
至testbar/.style
進行這些變更後,新的輸出如下所示。