Diagramm mit sortierten Schwebenden Säulen erstellen

Tabellen und Grafiken erstellen und anordnen


Zoid
Forum-Century
Forum-Century
Beiträge: 104
Registriert: Do 12. Mär 2020, 13:48

Diagramm mit sortierten Schwebenden Säulen erstellen

Beitrag von Zoid »

Moin moin,

ich würde gerne ein Diagramm erstellen, indem ich mittels schwebenden Säulen bestimmte Wertebereiche in Gruppen anzeigen kann. Dabei habe ich Teil A,B,C welche unterschiedliche Merkmale in X,Y,Z aufweisen. Die schwebenden Säulen sollen dabei zwischen dem Minimum(y-) und Maximum(y+) verlaufen, beim Mittelwert(yo) eine Linie aufweisen, sowie ebenfalls nach Teil A,B,C gruppiert werden.

Ich habe leider keine Beispiele für schwebende Säulen in einem Balkendiagramm gefunden. Mein bisheriger Code sieht aktuell so aus, ist aber noch nicht sehr zielführend.

\documentclass[english,12pt,a4paper]{scrreprt}
\usepackage{biblatex}	% Geht nicht ohne. Warum auch immer ...
\usepackage{color, colortbl}	% Tabelle Zeilen oder Spalten Hintergrundfarbe
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
	
\begin{document}
\chapter{Chapter}

\begin{figure}[htb]
\centering
\begin{tikzpicture}
\pgfmathsetlengthmacro\balkenbreite{20mm}
\definecolor{X}{HTML}{75C36C} 
\definecolor{Y}{HTML}{F78181}
\definecolor{Z}{HTML}{F83940} 
\pgfplotstableread[col sep=comma]{
x,	yX-,	yXo,	yX+,	yY-,	yYo,	yY+,	yZ-,	yZo,	yZ+
1,	3,		5,		9,		2,		4,		8,		3,		6,		8
2,	2,		3,		7,		3,		4,		6,		3,		6,		8
3,	1,		4,		8,		1,		3,		5,		2,		4,		7
}\data
\begin{axis}[
	width=15cm,
	height=7.5cm,
	axis x line=bottom,
	axis y line=left,
	ybar,
	xtick={data},
	xticklabels={A,B,C},
	ymin=0,
	ymax=10,
	ytick={0,1,2,3,4,5,6,7,8,9},
	enlarge x limits=0.2,
	ymajorgrids
]
\addplot [fill=X,bar shift=-0.625\balkenbreite,bar width=0.25\balkenbreite,font=\footnotesize] table [y=yX-] {\data};
\addplot [fill=X,bar shift=-0.375\balkenbreite,bar width=0.25\balkenbreite,font=\footnotesize] table [y=yX+] {\data};
\addplot [fill=Y,bar shift=-0.125\balkenbreite,bar width=0.25\balkenbreite,font=\footnotesize] table [y=yY-] {\data};
\addplot [fill=Y,bar shift=0.125\balkenbreite,bar width=0.25\balkenbreite,font=\footnotesize] table [y=yY+] {\data};
\addplot [fill=Z,bar shift=0.375\balkenbreite,bar width=0.25\balkenbreite,font=\footnotesize] table [y=yZ-] {\data};
\addplot [fill=Z,bar shift=0.625\balkenbreite,bar width=0.25*\balkenbreite,font=\footnotesize] table [y=yZ+] {\data};

\end{axis}
\end{tikzpicture}
\caption{Caption}
\label{fig:p5}
\end{figure}

\end{document}
T1.PNG

Den schwarz gestrichelten Bereich hätte ich gerne weg und halt noch eine Linie für "yo" eingezeichnet. Die Säulen bekomme ich dann schon noch zusammengeschoben

Jemand Ideen, wo ich da nachlesen könnte bzw. wie ich das umsetzen könnte?

Vielen Dank und viele Grüße :D


Zoid
Forum-Century
Forum-Century
Beiträge: 104
Registriert: Do 12. Mär 2020, 13:48

Re: Diagramm mit sortierten Schwebenden Säulen erstellen

Beitrag von Zoid »

Ich hab mal selber weiter rumprobiert und mir eine Vorlage für ein Boxplot genommen und bearbeitet. Dadurch ergibt sich ein ganz anderer Ansatz. Soweit sieht es schon mal besser aus, allerdings gibt es noch einiges was verändert werden müsste. Beim Erstellen der Grafik konnte ich den Median und unteren Whisker löschen, allerdings nicht den oberen. Diesen konnte ich zwar mit draw=none abschwächen, allerdings finden sich noch diagonale Linien vom unteren Ende bei X zu den unteren Enden von Z. Des weiteren übernimmt die Legende leider nicht die Farben der Säulen, sodass eine Zuordnung schwer ist.

\documentclass[english,12pt,a4paper]{scrreprt}
\usepackage{biblatex}	% Geht nicht ohne. Warum auch immer ...
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\pgfplotsset{
    box plot/.style={
        /pgfplots/.cd,
        only marks,
        mark size=\pgfkeysvalueof{/pgfplots/box plot width},
        /pgfplots/error bars/y dir=plus,
        /pgfplots/error bars/y explicit,
    },
    box plot box/.style={mark=-,
        /pgfplots/error bars/draw error bar/.code 2 args={%
            \draw  ##1 -- ++(\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##2 -- ++(-\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##1 -- cycle;
        },
        /pgfplots/table/.cd,
        y index=\pgfkeysvalueof{/pgfplots/box plot box top index},
        y error expr={
            \thisrowno{\pgfkeysvalueof{/pgfplots/box plot box bottom index}}
            - \thisrowno{\pgfkeysvalueof{/pgfplots/box plot box top index}}
        },
        /pgfplots/box plot
    },
    box plot top whisker/.style={draw=none},
    box plot average/.style={color=red,mark=-,nodes near coords,every node near coord/.append style={yshift=3mm,font=\footnotesize,anchor=center,/pgf/number format/1000 sep=},
        /pgfplots/box plot,
        /pgfplots/table/y index=\pgfkeysvalueof{/pgfplots/box plot average index}
    },
    box plot width/.initial=1em,
    box plot x index/.initial=0,
    box plot box top index/.initial=2,
    box plot box bottom index/.initial=3,
    box plot average index/.initial=6,
}
\newcommand{\boxplot}[2][]{
    \addplot [box plot box,#1] table {#2};
    \addplot [box plot top whisker,#1] table {#2};
    \addplot [box plot average,#1] table {#2};
}



\begin{document}

\begin{tikzpicture}
\pgfplotsset{/pgf/number format/.cd, fixed,  assume math mode}
\begin{axis} [box plot width=2.5mm,
				width=15cm,
				height=7.5cm,
				ymin=0,
				ymax=10,
				xmin=0,
				xmax=4,
				xtick={data},
				xticklabels={X,Y,Z},
				axis x line*=bottom,
				axis y line=left,
				ymajorgrids,
				legend entries={A,B,C}
			]
			
	\boxplot [fill=blue,
xshift=-5mm,
box plot box bottom index=1,
box plot box top index=3,
box plot average index=2
	] {subdata/test.dat}
	
	\boxplot [fill=green,
box plot box bottom index=4,
box plot box top index=6,
box plot average index=5
	] {subdata/test.dat}
	
\boxplot [fill=yellow,
xshift=5mm,
box plot box bottom index=7,
box plot box top index=9,
box plot average index=8
] {subdata/test.dat}

\end{axis}
\end{tikzpicture}


\end{document}

Der Inhalt von test.dat ist:

1	3	5	9	2	4	8	3	6	8
2	2	3	7	3	4	6	3	6	8
3	1	4	8	1	3	5	2	4	7

Das Ganze sieht so aus:

t2.PNG

Der Eintrag (Zeile 40)

\addplot [box plot top whisker,#1] table {#2};

lässt sich leider auch nicht aus dem Code entfernen, sonst könnte ich mir vorstellen, dass damit die diagonalen Linien entfernt werden könnten.
Wie könnte ich die Linien sonst loswerden und die Legende richtig gestalten?


Zoid
Forum-Century
Forum-Century
Beiträge: 104
Registriert: Do 12. Mär 2020, 13:48

Re: Diagramm mit sortierten Schwebenden Säulen erstellen

Beitrag von Zoid »

Die Whisker Striche bekommt man weg, indem man

    box plot top whisker/.style={draw=none},

und

    \addplot [box plot top whisker,#1] table {#2};

löscht.

Fehlen nur noch die Legendeneinträge.


Antworten