Überarbeitungsverlauf[Zurück]
Klicke auf Einblenden/Ausblenden von Überarbeitungen 3

09 Apr '20, 09:55

esdd's gravatar image

esdd
17.8k304257

Wenn Du wenigstens `compat=1.9` setzt, dann lässt sich der Code deutlich vereinfachen. Ab dieser Version ist `stacked ignores zero=true` automatisch gesetzt. Die aktuelle `pgfplots` Version ist allerdings 1.17. Beim eingefrorenen TL2019 ist sie 1.16, weshalb ich in den Beispielen diesen Wert für `compat` verwende. [![alt text][1]][2] \documentclass[a4paper,12pt]{article} \usepackage{pgfplotstable}% lädt pgfplots, tikz, etw \pgfplotsset{compat=1.16}% mindestens 1.9 und unbedingt vor allen anderen pgfplots Einstellungen setzen setzen, alternativ geht auch compat=newest \pgfplotsset{ percentage series/.style={ table/y expr=(\thisrow{#1}/\thisrow{sum}*100), table/meta=#1, table/x expr=\coordindex, }, percentage plot/.style={ axis line style={draw=none},% Achsen nicht zeichnen tick pos=lower,% y-Ticks links, x-Ticks unten tick align=outside,% Ticks nach außen antragen, tick style={draw=none},% aber nicht zeichnen % Beschriftung in Balken: nodes near coords, point meta=explicit, every node near coord/.append style={text=black}, % Bereich und Beschriftung der y-Achse: ymin=0,ymax=100, ytick={0,20,...,100}, yticklabel=\pgfmathprintnumber{\tick}\,$\%$, % Gitterlinien für y-Ticks ymajorgrids=true, grid style={gray} } } \pgfplotstableread[col sep=comma,header=false]{ TB,11,39 FB,23,26 IG,11,38 SFB,12,38 TB,6,15 FB,12,9 IG,11,8 SFB,17,1 }\data \pgfplotstablecreatecol[ create col/expr={\thisrow{1} + \thisrow{2}} ]{sum}{\data} \begin{document} \begin{tikzpicture} \begin{axis}[ ybar stacked, width=13cm, height=10cm, percentage plot, bar width=0.65cm, % Bereich und Beschriftung der x-Achse: xmin=-.5,xmax=7.5, xtick=data, xticklabels from table={\data}{[index]0}, % zusätzliche Markierungen an x-Achse minor xtick={-.5,3.5,7.5}, minor tick length=1.5cm, minor x tick style={draw,/pgfplots/every axis grid}, % zusätzliche Beschriftungen an x-Achse extra x ticks={1.5,5.5}, extra x tick labels={3-jährige, 4-jährige}, extra x tick style={ major tick length=\pgfkeysvalueof{/pgfplots/minor tick length}, tick label style={anchor=south}, }, % zusätzliche Beschriftung an y-Achse extra y ticks={50}, extra y tick style={% tick label style={red}, grid style={red} }, % Legende legend style={ at={(0.5,\dimexpr-\pgfkeysvalueof{/pgfplots/minor tick length}-.5cm\relax)}, anchor=north, legend columns=-2 }, ] % Balken \addplot table [percentage series=1] {\data}; \addplot table [percentage series=2] {\data}; %Legende \legend{\strut leere Box,\strut volle Box} \end{axis} \end{tikzpicture} \end{document} Oder wenn die Markierung für das Zufallsniveau über die Balken gezeichnet werden soll: [![alt text][3]][3] \documentclass[a4paper,12pt]{article} \usepackage{pgfplotstable}% lädt pgfplots, tikz, etw \pgfplotsset{compat=1.16}% mindestens 1.9 und unbedingt vor allen anderen pgfplots Einstellungen setzen setzen, alternativ geht auch compat=newest \pgfplotsset{ percentage series/.style={ table/y expr=(\thisrow{#1}/\thisrow{sum}*100), table/meta=#1, table/x expr=\coordindex, }, percentage plot/.style={ axis line style={draw=none},% Achsen nicht zeichnen tick pos=lower,% y-Ticks links, x-Ticks unten tick align=outside,% Ticks nach außen antragen, tick style={draw=none},% aber nicht zeichnen % Beschriftung in Balken: nodes near coords, point meta=explicit, every node near coord/.append style={text=black}, % Bereich und Beschriftung der y-Achse: ymin=0,ymax=100, ytick={0,20,...,100}, yticklabel=\pgfmathprintnumber{\tick}\,$\%$, % Gitterlinien für y-Ticks ymajorgrids=true, grid style={draw=none} } } \pgfplotstableread[col sep=comma,header=false]{ TB,11,39 FB,23,26 IG,11,38 SFB,12,38 TB,6,15 FB,12,9 IG,11,8 SFB,17,1 }\data \pgfplotstablecreatecol[ create col/expr={\thisrow{1} + \thisrow{2}} ]{sum}{\data} \begin{document} \begin{tikzpicture} \begin{axis}[ ybar stacked, width=13cm, height=10cm, percentage plot, bar width=0.65cm, % Bereich und Beschriftung der x-Achse: xmin=-.5,xmax=7.5, xtick=data, xticklabels from table={\data}{[index]0}, % zusätzliche Markierungen an x-Achse minor xtick={-.5,3.5,7.5}, minor tick length=1.5cm, minor x tick style={draw,/pgfplots/every axis grid}, % zusätzliche Beschriftungen an x-Achse extra x ticks={1.5,5.5}, extra x tick labels={3-jährige, 4-jährige}, extra x tick style={ major tick length=\pgfkeysvalueof{/pgfplots/minor tick length}, tick label style={anchor=south}, }, % zusätzliche Beschriftung an y-Achse extra y ticks={50}, extra y tick style={% tick label style={red}, grid style={red} }, % Legende legend style={ at={(0.5,\dimexpr-\pgfkeysvalueof{/pgfplots/minor tick length}-.5cm\relax)}, anchor=north, legend columns=-2 }, ] % wenn Linie über den Balken sein soll, muss sie zuerst angegeben werden %\draw[red](current axis.west|-0,50)--(current axis.east|-0,50); % Balken \addplot table [percentage series=1] {\data}; \addplot table [percentage series=2] {\data}; %Legende \legend{\strut leere Box,\strut volle Box} \end{axis} \end{tikzpicture} \end{document} [1]: https://texwelt.de/upfiles/tw_ybarstacked.png [2]: https://texwelt.de/upfiles/tw_ybarstacked.png [3]: https://texwelt.de/upfiles/tw_ybarstacked1.png
Klicke auf Einblenden/Ausblenden von Überarbeitungen 2

08 Apr '20, 10:14

esdd's gravatar image

esdd
17.8k304257

Wenn Du wenigstens `compat=1.9` setzt, dann lässt sich der Code deutlich vereinfachen. Ab dieser Version ist `stacked ignores zero=true` automatisch gesetzt. Die aktuelle `pgfplots` Version ist allerdings 1.17. Beim eingefrorenen TeXLive 2019 TL2019 ist sie 1.16, weshalb ich in den Beispielen diesen Wert für `compat` verwende. [![alt text][1]][2] \documentclass[a4paper,12pt]{article} \usepackage{pgfplotstable}% lädt pgfplots, tikz, etw \pgfplotsset{compat=1.16}% mindestens 1.9 und unbedingt vor allen anderen pgfplots Einstellungen setzen \pgfplotsset{ percentage series/.style={ table/y expr=(\thisrow{#1}/\thisrow{sum}*100), table/meta=#1, table/x expr=\coordindex, }, percentage plot/.style={ axis line style={draw=none},% Achsen nicht zeichnen tick pos=lower,% y-Ticks links, x-Ticks unten tick align=outside,% Ticks nach außen antragen, tick style={draw=none},% aber nicht zeichnen % Beschriftung in Balken: nodes near coords, point meta=explicit, every node near coord/.append style={text=black}, % Bereich und Beschriftung der y-Achse: ymin=0,ymax=100, ytick={0,20,...,100}, yticklabel=\pgfmathprintnumber{\tick}\,$\%$, % Gitterlinien für y-Ticks ymajorgrids=true, grid style={gray} } } \pgfplotstableread[col sep=comma,header=false]{ TB,11,39 FB,23,26 IG,11,38 SFB,12,38 TB,6,15 FB,12,9 IG,11,8 SFB,17,1 }\data \pgfplotstablecreatecol[ create col/expr={\thisrow{1} + \thisrow{2}} ]{sum}{\data} \begin{document} \begin{tikzpicture} \begin{axis}[ ybar stacked, width=13cm, height=10cm, percentage plot, bar width=0.65cm, % Bereich und Beschriftung der x-Achse: xmin=-.5,xmax=7.5, xtick=data, xticklabels from table={\data}{[index]0}, % zusätzliche Markierungen an x-Achse minor xtick={-.5,3.5,7.5}, minor tick length=1.5cm, minor x tick style={draw,/pgfplots/every axis grid}, % zusätzliche Beschriftungen an x-Achse extra x ticks={1.5,5.5}, extra x tick labels={3-jährige, 4-jährige}, extra x tick style={ major tick length=\pgfkeysvalueof{/pgfplots/minor tick length}, tick label style={anchor=south}, }, % zusätzliche Beschriftung an y-Achse extra y ticks={50}, extra y tick style={% tick label style={red}, grid style={red} }, % Legende legend style={ at={(0.5,\dimexpr-\pgfkeysvalueof{/pgfplots/minor tick length}-.5cm\relax)}, anchor=north, legend columns=-2 }, ] % Balken \addplot table [percentage series=1] {\data}; \addplot table [percentage series=2] {\data}; %Legende \legend{\strut leere Box,\strut volle Box} \end{axis} \end{tikzpicture} \end{document} Oder wenn die Markierung für das Zufallsniveau über die Balken gezeichnet werden soll: [![alt text][3]][3] \documentclass[a4paper,12pt]{article} \usepackage{pgfplotstable}% lädt pgfplots, tikz, etw \pgfplotsset{compat=1.16}% mindestens 1.9 und unbedingt vor allen anderen pgfplots Einstellungen setzen \pgfplotsset{ percentage series/.style={ table/y expr=(\thisrow{#1}/\thisrow{sum}*100), table/meta=#1, table/x expr=\coordindex, }, percentage plot/.style={ axis line style={draw=none},% Achsen nicht zeichnen tick pos=lower,% y-Ticks links, x-Ticks unten tick align=outside,% Ticks nach außen antragen, tick style={draw=none},% aber nicht zeichnen % Beschriftung in Balken: nodes near coords, point meta=explicit, every node near coord/.append style={text=black}, % Bereich und Beschriftung der y-Achse: ymin=0,ymax=100, ytick={0,20,...,100}, yticklabel=\pgfmathprintnumber{\tick}\,$\%$, % Gitterlinien für y-Ticks ymajorgrids=true, grid style={draw=none} } } \pgfplotstableread[col sep=comma,header=false]{ TB,11,39 FB,23,26 IG,11,38 SFB,12,38 TB,6,15 FB,12,9 IG,11,8 SFB,17,1 }\data \pgfplotstablecreatecol[ create col/expr={\thisrow{1} + \thisrow{2}} ]{sum}{\data} \begin{document} \begin{tikzpicture} \begin{axis}[ ybar stacked, width=13cm, height=10cm, percentage plot, bar width=0.65cm, % Bereich und Beschriftung der x-Achse: xmin=-.5,xmax=7.5, xtick=data, xticklabels from table={\data}{[index]0}, % zusätzliche Markierungen an x-Achse minor xtick={-.5,3.5,7.5}, minor tick length=1.5cm, minor x tick style={draw,/pgfplots/every axis grid}, % zusätzliche Beschriftungen an x-Achse extra x ticks={1.5,5.5}, extra x tick labels={3-jährige, 4-jährige}, extra x tick style={ major tick length=\pgfkeysvalueof{/pgfplots/minor tick length}, tick label style={anchor=south}, }, % zusätzliche Beschriftung an y-Achse extra y ticks={50}, extra y tick style={% tick label style={red}, grid style={red} }, % Legende legend style={ at={(0.5,\dimexpr-\pgfkeysvalueof{/pgfplots/minor tick length}-.5cm\relax)}, anchor=north, legend columns=-2 }, ] % wenn Linie über den Balken sein soll, muss sie zuerst angegeben werden %\draw[red](current axis.west|-0,50)--(current axis.east|-0,50); % Balken \addplot table [percentage series=1] {\data}; \addplot table [percentage series=2] {\data}; %Legende \legend{\strut leere Box,\strut volle Box} \end{axis} \end{tikzpicture} \end{document} [1]: https://texwelt.de/upfiles/tw_ybarstacked.png [2]: https://texwelt.de/upfiles/tw_ybarstacked.png [3]: https://texwelt.de/upfiles/tw_ybarstacked1.png
Klicke auf Einblenden/Ausblenden von Überarbeitungen 1

08 Apr '20, 10:13

esdd's gravatar image

esdd
17.8k304257