\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
この試みは、2 つの解決策を提供しようとしています。スタッキングとは、連続するコマンドの x 座標または y 座標を\addplot
互いの上に追加すること (ここでは x 方向) を意味し、特に 90% と 3% の重複が発生します。
一番上の最初の解決策は、ラベルを -90 度回転し、小さいフォント サイズを使用することです。これを行うには、every node near coord/.append style
コマンドを使用して追加スタイルを定義します。shift コマンドは、ラベルがボックスと衝突しないようにラベルを上に移動して読みやすくするためのものであることに注意してくださいshift={(axis direction cs:-3,0)}
。
下の 2 番目のプロットは、デフォルトのラベルの方向を変更せずに、y 座標を同じ y=0 から y=0,1,2,3,4,5 に変更するだけで、ラベルを階段状にします。さらに、このプロットでは、ラベルが 1 つの水平に積み上げられたバーに折りたたまれると、ラベルが重なる理由がわかります。
凡例に関しては、凡例スタイルは を介して定義されます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
これらの変更を加えると、新しい出力が以下のように表示されます。