PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : pgfplots: Säulendiagramm immer 2 nebeneinander ohne Abstand



ElGato
11-01-2015, 13:04
Liebes Forum,

ich habe eine Excel-Tabelle, die im Prinzip so aussieht:


Probe1 Probe2
Gruppe MW SD MW SD
Hans 3 1 4 1
Peter 4 1 5 2
Sven 5 2 6 1
Tim 6 3 7 2
Max 7 4 8 2


Ziel ist es immer 2 Säulen mit Mittelwert (MW) uns Standartabweichung (SD) nebeneinander zu setzen, z.B. für Hans die Säule von Probe1 und Probe2 direkt nebeneinander, dann mit etwas Abstand die beiden Säulen für Peter usw. Die Säulen der Probe1 sollten schwarz und die von Probe2 weiss sein.

Aus vorherigen Threads in diesem Forum, bin ich soweit gekommen:



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

\begin{filecontents}{Daten.dat}
Gruppe MW SD MW SD
Hans 3 1 4 1
Peter 4 1 5 2
Sven 5 2 6 1
Tim 6 3 7 2
Max 7 4 8 2
\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=10,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] {Daten.dat};
}

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

esdd
11-01-2015, 14:28
Ich fürchte, dass ich nicht ganz verstehe, was Du möchtest, da Du im Text von einem Säulendiagramm sprichst und dann aber groupplots verwendest. In jedem Fall müssen die Spalten in der Tabelle unterschiedlich bezeichnet werden, also z.B. MW1 und SD1 sowie MW2 und SD2.


\documentclass{scrbook}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=1.4}% warum verwendest Du nicht die aktuelle 1.11

\begin{filecontents}{Daten.dat}
Gruppe MW1 SD1 MW2 SD2
Hans 3 1 4 1
Peter 4 1 5 2
Sven 5 2 6 1
Tim 6 3 7 2
Max 7 4 8 2
\end{filecontents}


\begin{document}

\begin{figure}[hbt!]
\centering
\begin{tikzpicture}
\begin{axis}[
height=6cm,
axis y line=left,ymin=0,
ybar=0pt,% Abstand zwischen schwarzen und weißen Säulen soll 0 sein
error bars/.cd,error bars/y explicit,error bars/y dir=plus,
axis x line*=bottom,xtick=\empty,
]
\addplot[fill=black] table [ x expr=\coordindex, y=MW1,y error=SD1] {Daten.dat};
\addplot[fill=white] table [ x expr=\coordindex, y=MW2,y error=SD2] {Daten.dat};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

Warum verwendest Du compat=1.4? Die aktuelle pgfplots Version ist 1.11.

Gruß
Elke

ElGato
11-01-2015, 14:56
Vielen Dank, Elke!
Dein Vorschlag ist perfekt!!!

Wenn ich ehrlich bin, habe ich das Erstellen Diagrammen in LaTeX bis heute nicht verstanden. Bisher hatte ich immer den Code über Copy&Paste verwendet und häufig durch try and error angepasst, von daher kann ich auch leider nicht Deine Fragen beantworten.

Selbstverständlich gäbe es die Möglichkeit, dass ich mich in das Paket einarbeite, aber ich nutze es so selten, dass sich vermutlich der Aufwand nicht lohnen würde. Bisher habe ich die Doku nur für kleine Korrekturen verwendet.

Nochmals vielen, vielen Dank für die Hilfe!!!

ElGato
11-01-2015, 16:59
Ich packe es nicht, die Säulen richtig zu beschriften :(

Wie erhalte ich für die ersten beiden Säulen unten statt 1 "Hans" usw.?

xtick=data ist schon mal ein Ansatz, aber das manuelle Ändern gelingt mir nicht :(

\documentclass{scrbook}
\usepackage{filecontents}
\usepackage{pgfplots}

\begin{document}
\begin{filecontents}{Daten.dat}
Gruppe MW SD MW SD
Hans 3 1 4 1
Peter 4 1 5 2
Sven 5 2 6 1
Tim 6 3 7 2
Max 7 4 8 2
\end{filecontents}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
height=6cm,
axis y line=left,ymin=0,ymax=3,ylabel=Foldchange,
ybar=0pt,% Abstand zwischen schwarzen und weißen Säulen soll 0 sein
error bars/.cd,error bars/y explicit,error bars/y dir=plus,
axis x line*=bottom,xtick=data,
x tick label style={align=center, yshift=0cm},
legend entries={1,2},
legend style={draw=none,/tikz/every even column/.append style={column sep=2
pt},font=\sffamily\footnotesize},legend cell align=left, %Position der Legende, xy
legend image code/.code={\draw[#1] (0cm,-0.1cm) rectangle (0.5cm,0.1cm);}
]

\addplot[fill=black] table [x expr=\coordindex, y=MW1,y error=SD1] {RT-PCR.dat};
\addplot[fill=white] table [ x expr=\coordindex, y=MW2,y error=SD2] {RT-PCR.dat};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

esdd
11-01-2015, 22:57
Bitte verwende zum Einfügen von Code den entsprechenden Button und teste Deine Beispiele vorher. Dein letztes war gleich aus mehreren Gründen nicht lauffähig und die Einstellung ymax=3 passte nicht zu den Daten.

Neben xtick=data musst Du noch xticklabels from table={Daten.dat}{Gruppe} setzen:


\documentclass{scrbook}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{filecontents}{Daten.dat}
Gruppe MW1 SD1 MW2 SD2
Hans 3 1 4 1
Peter 4 1 5 2
Sven 5 2 6 1
Tim 6 3 7 2
Max 7 4 8 2
\end{filecontents}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
height=6cm,
axis y line=left,ymin=0,ymax=10,ylabel=Foldchange,
ybar=0pt,% Abstand zwischen schwarzen und weißen Säulen soll 0 sein
error bars/.cd,error bars/y explicit,error bars/y dir=plus,
axis x line*=bottom,xtick=data,xticklabels from table={Daten.dat}{Gruppe},
x tick label style={align=center, yshift=0cm},
legend entries={1,2},
legend style={draw=none, column sep=2pt, font=\sffamily\footnotesize},
legend cell align=left,
legend image code/.code={\draw[#1] (0cm,-0.1cm) rectangle (10pt,0.1cm);}
]

\addplot[fill=black] table [x expr=\coordindex,y=MW1,y error=SD1]{Daten.dat};
\addplot[fill=white] table [x expr=\coordindex,y=MW2,y error=SD2]{Daten.dat};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}


Gruß
Elke

ElGato
18-01-2015, 16:33
Super! Vielen Dank!!!

Nun müsste ich bei einigen Säulen ganz oben ein * setzen, um zu zeigen, dass die Säule significant ist.



\documentclass{scrbook}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{calc} % Berechnungen von z.B. Breite für Bilder
\usetikzlibrary{pgfplots.groupplots}
\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},
select row/.style={ x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult {}\fi}},
ylabsh/.style={every axis y label/.style={at={(0,0.5)}, xshift=#1, rotate=90}}
}

\begin{document}
\begin{filecontents}{Daten.dat}
Gruppe MW1 SD1 MW2 SD2
Hans 3 1 4 1
Peter 4 1 5 2
Sven 5 2 6 1
Tim 6 3 7 2
Max 7 4 8 2
\end{filecontents}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
title={{\sffamily\bfseries Ueberschrift Test}},
height=8cm,
width=10cm,
bar width=0.65cm,
axis y line=left,ymin=0,ymax=15,ylabel=Y-Achse,
ybar=0pt,% Abstand zwischen schwarzen und weißen Säulen soll 0 sein
error bars/.cd,error bars/y explicit,error bars/y dir=plus,
axis x line*=bottom,xtick=data,
x tick label style={align=center, yshift=0cm,rotate=45},
xticklabels from table={Daten.dat}{Gruppe},
legend entries={1,2},
legend style={at={(1.2,0.075)},draw=none,/tikz/every even column/.append style={column sep=2pt},font=\sffamily\footnotesize},legend cell align=left, %Position der Legende, xy
legend image code/.code={\draw[#1] (0cm,-0.1cm) rectangle (0.5cm,0.1cm);}
]
\addplot[fill=black] table [x expr=\coordindex,x=Gruppe, y=MW1,y error=SD1] {Daten.dat};
\addplot[fill=white] table [ x expr=\coordindex,x=Gruppe, y=MW2,y error=SD2] {Daten.dat};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}


Hier wäre es gut so etwas zu haben wie



\begin{filecontents}{Daten.dat}
Gruppe MW1 SD1 Label1 MW2 SD2 Label2
Hans 3 1 $star$ 4 1 \
Peter 4 1 \ 5 2 \
Sven 5 2 \ 6 1 $star$
Tim 6 3 $star$ 7 2 $star$
Max 7 4 \ 8 2 \
\end{filecontents}


Sorry, hatte die Code-Umgebung vergessen und vergessen das Beispiel zu testen :$ Jetzt müsste alles passen.

esdd
20-01-2015, 15:23
Mit der pgfplots Version 1.11 funktioniert bei mir das folgende:


\documentclass{scrbook}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.11,% <- wichtig, mindestens Version 1.11
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},
%select row/.style={ x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult {}\fi}},
%ylabsh/.style={every axis y label/.style={at={(0,0.5)}, xshift=#1, rotate=90}}
}

\begin{document}
\begin{filecontents}{Daten.dat}
Gruppe MW1 SD1 MW2 SD2 Label
Hans 3 1 4 1 {}
Peter 4 1 5 2 {}
Sven 5 2 6 1 $\star$
Tim 6 3 7 2 $\star$
Max 7 4 8 2 {}
\end{filecontents}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
title={{\sffamily\bfseries Ueberschrift Test}},
height=8cm,
width=10cm,
bar width=0.65cm,
axis y line=left,ymin=0,ymax=15,ylabel=Y-Achse,
ybar=0pt,% Abstand zwischen schwarzen und weißen Säulen soll 0 sein
error bars/.cd,error bars/y explicit,error bars/y dir=plus,
axis x line*=bottom,xtick=data,
x tick label style={align=center, yshift=0cm,rotate=45},
xticklabels from table={Daten.dat}{Gruppe},
legend entries={1,2},
legend style={at={(1.2,0.075)},draw=none,/tikz/every even column/.append style={column sep=2pt},font=\sffamily\footnotesize},legend cell align=left, %Position der Legende, xy
legend image code/.code={\draw[#1] (0cm,-0.1cm) rectangle (0.5cm,0.1cm);},
%%% neu:
point meta=explicit symbolic,
nodes near coords,
nodes near coords align={vertical},
every node near coord/.append style={
anchor=center,shift={(0,\myshift)},font={\bfseries \huge}}
%%%
]
\addplot[fill=black,visualization depends on=\thisrow{SD1}+1\as \myshift]
table [x expr=\coordindex,x=Gruppe, y=MW1,y error=SD1,meta=Label] {Daten.dat};
\addplot[fill=white,visualization depends on=\thisrow{SD2}+1\as \myshift,]
table [ x expr=\coordindex,x=Gruppe, y=MW2,y error=SD2,meta=Label] {Daten.dat};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

6103

Gruß
Elke

ElGato
20-01-2015, 19:54
Hallo Elke,

vielen Dank!!!

Ich habe mehrere Stunden mit verschiedenen Code-Schnipseln versucht das hinzukriegen.

Dein Vorschlag ist wirklich perfekt für mich :)

Nochmals vielen Dank!