\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}
Ich wollte ein gestapeltes horizontales Balkendiagramm mit einer Legende und dem Prozentsatz jedes Teils erstellen. Der obige Code gibt mir Folgendes:
Aber wie Sie sehen, sind die Prozentsätze alle Null. Ich möchte auch eine Legende. Vielen Dank im Voraus.
BEARBEITEN
Ich habe den Code mit Jesses Hilfe geändert, sodass mein Code jetzt wie folgt lautet:
\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}
Aber das Bild, das es mir gibt, ist jetzt
Ich habe alle Pakete mit TeX Live Utilty aktualisiert und mein TeX-Editor ist TeXpad.
Antwort1
Dieser Versuch versucht, zwei Lösungen bereitzustellen. Da Stapeln bedeutet, entweder die x- oder die y-Koordinaten aufeinanderfolgender \addplot
Befehle übereinander zu legen (hier ist es die x-Richtung), kommt es insbesondere bei 90 % und 3 % zu Überlappungen.
Die erste Lösung oben besteht darin, das Etikett um -90 Grad zu drehen und die kleine Schriftgröße zu verwenden. Dazu muss per every node near coord/.append style
Befehl ein Anfügestil definiert werden. Es wird darauf hingewiesen, dass der Umschaltbefehl dazu shift={(axis direction cs:-3,0)}
dient, die Etiketten nach oben zu verschieben, damit sie nicht mit den Kästchen kollidieren und so die Lesbarkeit verbessern.
Das zweite Diagramm unten ändert einfach die y-Koordinaten von y=0 zu y=0,1,2,3,4,5 und macht sie so zu einer Treppe, ohne die Standardausrichtung der Beschriftungen zu ändern. Darüber hinaus können Sie in diesem Diagramm beobachten, warum sich die Beschriftungen überlagern, wenn sie zu einem einzigen horizontal gestapelten Balken zusammenfallen.
Für die Legende wird ein Legendenstil über definiert 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}
Bearbeiten:
Der OP bevorzugt die Verwendung von Prozentsätzen statt Zahlen. Ersetzen Sie dann die entsprechenden Zeilen durch
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
.Um den Titel weiter nach oben zu verschieben, fügen Sie
title style={yshift=10pt}
der Achse des ersten Diagramms etwas hinzu, da der hinzugefügte %-Wert mit dem Titel in der alten Einstellung kollidieren würde.Um das Gitter oben zu entfernen, fügen Sie
axis on top=false
demtestbar/.style
Nach diesen Änderungen wird unten eine neue Ausgabe angezeigt.