PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : pgfplots: Mehrere mehrfarbiges Säulendiagramme mit einer gemeinsamen Legende



ElGato
30-01-2013, 18:57
Liebes Forum,

ich habe 4 Diagramme mit 5 items. Wie stelle ich eine Säulenfarbe für ein item ein und wie erhalte ich für alle eine gemeinsame Legende?

Laut diesem Post (http://tex.stackexchange.com/questions/36787/pgfplots-subplots-with-common-legend) ist es wohl möglich, nur habe ich überhaupt keine Ahnung, wie ich anfangen soll :confused:



\documentclass{scrbook}
\usepackage{xltxtra} % XeLaTeX
\usepackage{pgfplots}
\pgfplotsset{every axis label/.append style={font=\sffamily\small},
every axis legend/.style={y tick label style={/pgf/number format/1000
sep=},x tick label style={/pgf/number format/1000 sep=}}, every axis
legend label/.append style={font=\sffamily\small},
every tick label/.append style={font=\sffamily\small},
every axis title/.append style={font=\sffamily\bfseries}}
\usepackage{filecontents}
\usepackage[font={sf,footnotesize},labelfont=bf,format=plain,
singlelinecheck=off,aboveskip=0.1em]{caption}
\usepackage{subfig}
\captionsetup[subfigure]{labelformat=simple}

\newlength{\abstand}\setlength{\abstand}{0.25cm}
\newlength{\diagramm}\setlength{\diagramm}{6.20cm}

\begin{document}

\begin{figure}
\begin{filecontents}{Woerter.dat}
X-Position Gruppe MW SD % Wörter
1 Hans 2788.433 707.484260009309 % (n=3)
2 Peter 3503.050 806.076 % (n=2)
3 Sven 3697.500 368.310 % (n=2)
4 Tim 3395.867 523.215 % (n=3)
5 Max 3697.032 513.163 % (n=3)
\end{filecontents}
\begin{filecontents}{Lieder.dat}
X-Position Gruppe MW SD % Lieder
1 Hans 2700.667 634.761 % (n=3)
2 Peter 1804.450 498.362 % (n=2)
3 Sven 1815.200 368.310 % (n=2)
4 Tim 2440.267 523.215 % (n=3)
5 Max 2112.548 513.163 % (n=3)
\end{filecontents}
\begin{filecontents}{Treffer.dat}
X-Position Gruppe MW SD % Teffer
1 Hans 1.104 0.446 % (n=3)
2 Peter 2.117 0.953 % (n=2)
3 Sven 2.110 0.455 % (n=2)
4 Tim 1.490 0.575 % (n=3)
5 Max 1.861 0.576 % (n=3)
\end{filecontents}
\begin{filecontents}{Ziele.dat}
X-Position Gruppe MW SD
1 Hans 10.6666666667 0.5773502692
2 Peter 10.6666666667 4.1633319989
3 Sven 8.6666666667 2.0816659995
4 Tim 8.3333333333 3.0550504633
5 Max 9 4.582575695
\end{filecontents}
\subfloat[]{\label{fig:Woerter}
\begin{tikzpicture}
\begin{axis}[
width=\diagramm,
axis x line*=bottom, axis y line=left,
ylabel near ticks,ylabel=Woerter,
ymin=0,ymax=4500,
ybar,bar width=20pt,
error bars/.cd, error bars/y dir=plus, error bars/y explicit,
xtick=data,
xticklabels from table={Woerter.dat}{Gruppe},
x tick label style={rotate=45}
]
\addplot[fill=black,draw=black]table[
x=X-Position,
y=MW,
y error=SD
]{Woerter.dat};
\end{axis}
\end{tikzpicture}
}\hfill\qquad
\subfloat[]{\label{}
\begin{tikzpicture}
\begin{axis}[
width=\diagramm,
axis x line*=bottom, axis y line=left,
ylabel near ticks,ylabel=Lieder/Tag,
ymin=0,ymax=4000,
ybar,bar width=20pt,
error bars/.cd, error bars/y dir=plus, error bars/y explicit,
xtick=data,
xticklabels from table={Lieder.dat}{Gruppe},
x tick label style={rotate=45}
]
\addplot[fill=black,draw=black]table[
x=X-Position,
y=MW,
y error=SD
]{Lieder.dat};
\end{axis}
\end{tikzpicture}
}

\subfloat[]{\label{fig:Treffer}
\begin{tikzpicture}
\begin{axis}[
width=\diagramm,
axis x line*=bottom, axis y line=left,
ylabel near ticks,ylabel=Treffer,
ymin=0,ymax=3.5,
ybar,bar width=20pt,
error bars/.cd, error bars/y dir=plus, error bars/y explicit,
xtick=data,
xticklabels from table={Treffer.dat}{Gruppe},
x tick label style={rotate=45}
]
\addplot[fill=black,draw=black]table[
x=X-Position,
y=MW,
y error=SD
]{Treffer.dat};
\end{axis}
\end{tikzpicture}
}\hfill\qquad
\subfloat[]{\label{fig:Ziele}
\begin{tikzpicture}
\begin{axis}[
width=\diagramm,
axis x line*=bottom, axis y line=left,
ylabel near ticks,ylabel=Ziele,
ymin=0,ymax=16,
ybar,bar width=20pt,
error bars/.cd, error bars/y dir=plus, error bars/y explicit,
xtick=data,
xticklabels from table={Ziele.dat}{Gruppe},
x tick label style={rotate=45}
]
\addplot[fill=black,draw=black]table[
x=X-Position,
y=MW,
y error=SD
]{Ziele.dat};
\end{axis}
\end{tikzpicture}
}
\caption{Caption-Text.}\label{fig:Bild}
\end{figure}
\end{document}

ElGato
30-01-2013, 22:19
Was ich bisher geschafft dank dieser Quelle (http://tex.stackexchange.com/questions/37568/colors-and-legend-in-groupplots) geschaft habe



\documentclass{scrbook}
\usepackage{xltxtra} % XeLaTeX
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.4}
\usetikzlibrary{pgfplots.groupplots}

\pgfplotstableread{
Gruppe MW SD
Hans 2788.433 707.484260009309
Peter 3503.050 806.076
Sven 3697.500 368.310
Tim 3395.867 523.215
Max 3697.032 513.163
}\datatable

\definecolor{RYB1}{RGB}{141, 211, 199}
\definecolor{RYB2}{RGB}{255, 255, 179}
\definecolor{RYB3}{RGB}{190, 186, 218}
\definecolor{RYB4}{RGB}{251, 128, 114}
\definecolor{RYB5}{RGB}{128, 177, 211}
\definecolor{RYB6}{RGB}{253, 180, 98}
\definecolor{RYB7}{RGB}{179, 222, 105}

\pgfplotscreateplotcyclelist{colorbrewer-RYB}{
{RYB1!50!black,fill=RYB1},
{RYB2!50!black,fill=RYB2},
{RYB3!50!black,fill=RYB3},
{RYB4!50!black,fill=RYB4},
{RYB5!50!black,fill=RYB5},
{RYB6!50!black,fill=RYB6},
{RYB7!50!black,fill=RYB7},
}

\pgfplotsset{
select row/.style={
x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult {}\fi}
}
}

\begin{document}

\begin{figure}[hbt!]
\centering
\begin{tikzpicture}
\begin{groupplot}
[
group style={
group size=2 by 2,
xlabels at=edge bottom,
ylabels at=edge left,group name=plots
},height=6cm,
axis x line*=bottom, axis y line=left,
ylabel near ticks,ymin=0,
ybar,error bars/.cd, error bars/y dir=plus, error bars/y explicit,
cycle list name=colorbrewer-RYB,
legend style={draw=none, /tikz/every even column/.append style={column sep=5pt}},
legend image code/.code={%
\draw[#1] (0cm,-0.1cm) rectangle (0.5cm,0.1cm);
}
]

\nextgroupplot[ymax=4500,ylabel=Lieder/Tag,legend to name=grouplegend]
\pgfplotsinvokeforeach {0,...,6}{
\addplot table [ x expr=\coordindex, select row=#1, y=MW,y error=SD] {\datatable};
\addlegendentry {\pgfplotstablegetelem{#1}{Gruppe}\of\datatable \pgfplotsretval}
}

\nextgroupplot[ymax=4500,ylabel=Tag/Tag]
\pgfplotsinvokeforeach {0,...,6}{
\addplot table [x expr=\coordindex, select row=#1, y=MW] {\datatable};
}

\nextgroupplot[ymax=1000]
\pgfplotsinvokeforeach {0,...,6}{
\addplot table [x expr=\coordindex, select row=#1, y=SD] {\datatable};
}

\end{groupplot}

\node at [inner sep=50pt,anchor=east, yshift=-5ex] {\ref{grouplegend}};
\end{tikzpicture}
\end{figure}
\end{document}


Einiges lieg noch im Argen, z.B. die Legende sollte ganz unten sein. Die Breite der Säulen könnte breiter sein und die Abstände untereinander geringer. Die X-Achse benötigt keine Beschriftung.

ElGato
31-01-2013, 11:16
Gelöst :)



\documentclass{scrbook}
\usepackage{xltxtra} % XeLaTeX
\usepackage{filecontents}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.4}
\usetikzlibrary{pgfplots.groupplots}

\begin{filecontents}{Woerter.dat}
Gruppe MW SD
Hans 2788.433 707.484260009309
Peter 3503.050 806.076
Sven 3697.500 368.310
Tim 3395.867 523.215
Max 3697.032 513.163
\end{filecontents}
\begin{filecontents}{Lieder.dat}
Gruppe MW SD
Hans 2700.667 634.761
Peter 1804.450 498.362
Sven 1815.200 368.310
Tim 2440.267 523.215
Max 2112.548 513.163
\end{filecontents}
\begin{filecontents}{Treffer.dat}
Gruppe MW SD
Hans 1.104 0.446
Peter 2.117 0.953
Sven 2.110 0.455
Tim 1.490 0.575
Max 1.861 0.576
\end{filecontents}
\begin{filecontents}{Ziele.dat}
Gruppe MW SD
Hans 10.6666666667 0.5773502692
Peter 10.6666666667 4.1633319989
Sven 8.6666666667 2.0816659995
Tim 8.3333333333 3.0550504633
Max 9 4.582575695
\end{filecontents}

\definecolor{Schwarz}{RGB}{0, 0, 0}
\definecolor{Grau1}{gray}{0.30}
\definecolor{Grau2}{gray}{0.60}
\definecolor{Grau3}{gray}{0.90}
\definecolor{Weiss}{RGB}{255, 255, 255}

\pgfplotscreateplotcyclelist{colorbrewer-Graustufen}{
{Schwarz!50!black,fill=Schwarz},
{Grau1!50!black,fill=Grau1},
{Grau2!50!black,fill=Grau2},
{Grau3!50!black,fill=Grau3},
{Weiss!50!black,fill=Weiss},
}

\pgfplotsset{
select row/.style={
x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult {}\fi}
}
}

\begin{document}

\begin{figure}[hbt!]
\centering
\begin{tikzpicture}
\begin{groupplot}
[
group style={group size=2 by 2,ylabels at=edge left,horizontal sep=3cm,group name=plots},
height=6cm,
axis y line=left,ylabel near ticks,ymin=0,ybar,error bars/.cd,error bars/y explicit,error bars/y dir=plus,/pgf/bar shift=1pt,
axis x line*=bottom,xtick=\empty,
cycle list name=colorbrewer-Graustufen,
legend style={at={(0.75,-0.15)},draw=none,/tikz/every even column/.append style={column sep=5pt}},legend columns=-1,
legend image code/.code={\draw[#1] (0cm,-0.1cm) rectangle (0.5cm,0.1cm);}
]

\nextgroupplot[ymax=4500,ylabel=Lieder/Tag,bar width=20pt,legend to name=grouplegend]
\pgfplotsinvokeforeach {0,...,5}{
\addplot table [ x expr=\coordindex, select row=#1, y=MW,y error=SD] {Woerter.dat};
}

\nextgroupplot[ymax=4500,ylabel=Tag/Tag,bar width=20pt]
\pgfplotsinvokeforeach {0,...,5}{
\addplot table [x expr=\coordindex, select row=#1, y=MW,y error=SD] {Lieder.dat};
}

\nextgroupplot[ymax=5,bar width=20pt]
\pgfplotsinvokeforeach {0,...,5}{
\addplot table [x expr=\coordindex, select row=#1, y=MW,y error=SD] {Treffer.dat};
}

\nextgroupplot[ymax=15,bar width=20pt]
\pgfplotsinvokeforeach {0,...,5}{
\addplot table [x expr=\coordindex, select row=#1, y=MW,y error=SD] {Ziele.dat};
\addlegendentry {\pgfplotstablegetelem{#1}{Gruppe}\of{Woerter.dat} \pgfplotsretval}
}

\end{groupplot}
\node at (plots c2r1.south) [inner sep=0pt,anchor=north, yshift=-5ex] {\ref{grouplegend}};
\end{tikzpicture}
\end{figure}
\end{document}