PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : pgfplot mit 2ter y-achse



someone25
24-03-2011, 11:48
Hallo zusammen,

ich würde gerne ein pgfplot wie im Anhang gezeigt erstellen, konnte leider nichts dazu im Forum oder in der Doku finden. Vielleicht kann mir jemand einen Tip geben.

Der in der Grafik gezeigte schwarze Teil habe ich wie folgt schon realisiert, der rote Teil ist die Herausforderung.




\pgfplotsset{width=0.9\textwidth,height=0.4\texthe ight,

xtick={1995,1999,2003,2007,2011},
x tick label style={/pgf/number format/1000 sep=},
xlabel={Jahr},
y tick label style={/pgf/number format/1000 sep=},
extra x ticks={1998},
extra x tick labels={{1998: Einführung}},
extra x tick style={grid=major, tick label style={rotate=90, xshift=+4.75cm, yshift=+0.25cm}},
enlarge x limits=0.01}


\centering
\begin{tikzpicture}
\begin{axis}
[legend pos=north west]

\addplot table [col sep=comma,trim cells=true,y=Mitarbeiter] {Entwicklung19952011.csv};
\addlegendentry{Anzahl}
\addplot table [col sep=comma,trim cells=true,y=Umsatz] {Entwicklung19952011.csv};
\addlegendentry{Umsatz Euro}

\end{axis}
\end{tikzpicture}
\caption{TEST}
\footnotesize Quelle: blupp

{\label{fig:1}}

\end{figure}





Vielen Dank schonmal im voraus!

borose
24-03-2011, 12:05
Hi,

schau lieber nochmal in die Doku :)

In Version 1.4.1 ist das bei 4.8.10 "Two Ordinates (y axis) or Multiple Axes" auf Seite 132.

Viele Güße

edit:



% Preamble: \pgfplotsset{width=7cm,compat=1.3}
\begin{tikzpicture}
\begin{axis}[
scale only axis,
xmin=-5,xmax=5,
axis y line*=left,% the ’*’ avoids arrow heads
xlabel=$x$,
ylabel=First ordinate]
\addplot {x^2};
\end{axis}
\begin{axis}[
scale only axis,
xmin=-5,xmax=5,
axis y line*=right,
axis x line=none,
ylabel=Second ordinate]
\addplot[red] {3*x};
\end{axis}
\end{tikzpicture}

someone25
24-03-2011, 12:45
Vielen Dank! :)

someone25
29-03-2011, 13:01
Hallo nochmal, mein Problem konnte noch nicht ganz gelöst werden.

Mit dem Beispiel aus der Doku überlagern sich die Achsen jetzt leider auf der linken Seite.




\begin{tikzpicture}
\begin{axis}
[legend pos=north west]

[scale only axis,
xmin=1995,xmax=2011,
ymin=0, ymax=1400,
axis y line*=right,% the ’*’ avoids arrow heads
ylabel=Anzahl der Mitarbeiter]
\addplot table [col sep=comma, trim cells=true, y=Mitarbeiter] {Entwicklung19952011.csv};
\addlegendentry{Anzahl der Mitarbeiter}
\end{axis}

\begin{axis}
[legend pos=south east]
[scale only axis,
xmin=1995,xmax=2011,
ymin=0, ymax=250,
axis y line*=right,
axis x line=none,
ylabel=Umsatz in Mio. Euro]
\addplot table [col sep=comma, trim cells=true, y=Umsatz] {Entwicklung19952011.csv};
% \addplot table [red, col sep=comma,trim cells=true,y=Umsatz] {Entwicklung19952011.csv};
\addlegendentry{Umsatz in Mio. Euro}
\end{axis}
\end{tikzpicture}


http://img713.imageshack.us/i/ergebnis.jpg

Kann mir jemand weiterhelfen?!

Vielen Dank

someone25
29-03-2011, 13:02
http://img713.imageshack.us/i/ergebnis.jpg

borose
30-03-2011, 10:48
ungetestet da kein Minimalbeispiel:




\begin{tikzpicture}
\begin{axis}
[legend pos=north west,
scale only axis,
xmin=1995,xmax=2011,
ymin=0, ymax=1400,
axis y line*=left,% the ’*’ avoids arrow heads
ylabel=Anzahl der Mitarbeiter]
\addplot table [col sep=comma, trim cells=true, y=Mitarbeiter] {Entwicklung19952011.csv};
\addlegendentry{Anzahl der Mitarbeiter}
\end{axis}

\begin{axis}
[legend pos=south east,
scale only axis,
xmin=1995,xmax=2011,
ymin=0, ymax=250,
axis y line*=right,
axis x line=none,
ylabel=Umsatz in Mio. Euro]
\addplot table [col sep=comma, trim cells=true, y=Umsatz] {Entwicklung19952011.csv};
% \addplot table [red, col sep=comma,trim cells=true,y=Umsatz] {Entwicklung19952011.csv};
\addlegendentry{Umsatz in Mio. Euro}
\end{axis}
\end{tikzpicture}

someone25
30-03-2011, 12:51
[QUOTE=borose;329189]ungetestet da kein Minimalbeispie l/QUOTE]

Vielen Dank, funktioniert. :)


Nur noch 2 Fragen:

1.) Es werden immer noch beide Label überlappend auf der linken Seite angezeigt. Welcher Befhl würde das ylabel auf der rechten Seite anzeigen lassen?

2.) Die Farben der Linien sind beide blau und mit Punkten. Wie bekomme ich für eine der beiden Kurven eine rote Linie mir Kästchen?


Viele Grüße

borose
30-03-2011, 14:11
Hi,

die Labels werden bei mir nicht überlagert. Poste mal ein lauffähiges Minimalbeispiel.



\begin{tikzpicture}
\begin{axis}
[legend pos=north west,
scale only axis,
xmin=1995,xmax=2011,
ymin=0, ymax=1400,
axis y line*=left,% the ’*’ avoids arrow heads
ylabel=Anzahl der Mitarbeiter]
\addplot coordinates {(1995,20) (2011,1400)};
\addlegendentry{Anzahl der Mitarbeiter}
\end{axis}
%
\begin{axis}
[legend pos=south east,
scale only axis,
xmin=1995,xmax=2011,
ymin=0, ymax=250,
axis y line*=right,
axis x line=none,
ylabel=Umsatz in Mio. Euro]
\addplot+[red] coordinates {(1995,20) (2011,200)};

\addlegendentry{Umsatz in Mio. Euro}
\end{axis}
\end{tikzpicture}%

someone25
30-03-2011, 14:34
Vielen Dank erstmal, leider schaffe ich auch das mit der Fabe nicht.

Anbei das Beispiel und die .csv Datei. (Mit Koordinatenpunkten war das Ergebnis wieder ganz anders!?)


\documentclass[12pt,a4paper,oneside, chapterprefix=false]{scrreprt}
\usepackage{pgfplots}
\begin{document}

\begin{figure}[ht] \centering

\pgfplotsset {width=0.9\textwidth,height=0.4\textheight,
xtick={1995,1999,2003,2007,2011},
x tick label style={/pgf/number format/1000 sep=},
xlabel={Jahr},
y tick label style={/pgf/number format/1000 sep=},
extra x ticks={1998},
extra x tick labels={{1998: Einführung}},
extra x tick style={grid=major, tick label style={rotate=90, xshift=+4.75cm, yshift=+0.25cm}},
%ylabel={Anzahl Mitarbeiter}
enlarge x limits=0.01}
\centering

\begin{tikzpicture}
\begin{axis}
[legend pos=north west, scale only axis,
xmin=1995,xmax=2011,
ymin=0, ymax=1400,
axis y line*=left,% the ’*’ avoids arrow heads
ylabel=Anzahl der Mitarbeiter]
\addplot table [col sep=comma, trim cells=true, y=Mitarbeiter] {Entwicklung19952011.csv};
\addlegendentry{Anzahl der Mitarbeiter}
\end{axis}

\begin{axis}
[legend pos=south east, scale only axis,
xmin=1995,xmax=2011,
ymin=0, ymax=250,
axis y line*=right,
axis x line=none,
ylabel=Umsatz in Mio. Euro]
\addplot table [col sep=comma, trim cells=true, y=Umsatz] {Entwicklung19952011.csv};
\addlegendentry{Umsatz in Mio. Euro}
\end{axis}

\end{tikzpicture}
\end{figure}
\end{document}

borose
30-03-2011, 16:03
Hi,

das mit dem überlagerten Label scheint ein Bug mit "axis y line*" zu sein. Bei Verwendung von "axis y line" funktionier tes. Allerdings sind da dann noch die störenden Pfeilspietzen dran, die noch entfernt werden müssen. Die Farbe lässt sich leicht ändern. Das ist auch sehr gut in der Doc beschrieben ...




\documentclass[12pt,a4paper,oneside, chapterprefix=false]{scrreprt}
\usepackage{pgfplots}
\begin{document}

\begin{figure}[ht] \centering

\pgfplotsset {width=0.9\textwidth,height=0.4\textheight,
xtick={1995,1999,2003,2007,2011},
x tick label style={/pgf/number format/1000 sep=},
xlabel={Jahr},
y tick label style={/pgf/number format/1000 sep=},
extra x ticks={1998},
extra x tick labels={{1998: Einführung}},
extra x tick style={grid=major, tick label style={rotate=90, xshift=+4.75cm, yshift=+0.25cm}},
%ylabel={Anzahl Mitarbeiter}
enlarge x limits=0.01}

\centering

\begin{tikzpicture}
\begin{axis}
[legend pos=north west, scale only axis,
xmin=1995,xmax=2011,
ymin=0, ymax=1400,
axis y line*=left,% the ’*’ avoids arrow heads
ylabel=Anzahl der Mitarbeiter]
\addplot table [col sep=comma, trim cells=true, y=Mitarbeiter] {Entwicklung19952011.csv};
\addlegendentry{Anzahl der Mitarbeiter}
\end{axis}

\begin{axis}
[legend pos=south east, scale only axis,
xmin=1995,xmax=2011,
ymin=0, ymax=250,
%
axis y line=right,
y axis line style={-},
%
axis x line=none,
ylabel=Umsatz in Mio. Euro]
\addplot+[red, mark options={fill=red}] table [col sep=comma, trim cells=true, y=Umsatz] {Entwicklung19952011.csv};
\addlegendentry{Umsatz in Mio. Euro}
\end{axis}

\end{tikzpicture}
\end{figure}
\end{document}

Grüße

someone25
20-04-2011, 07:58
Hallo nochmal,

ist es möglich bei diesem Beispiel, die Linie zwischen den ersten Punkten anderst darzustellen (z.B. gestrichelt) ?

Also zwischen Punkt 1 und 2 "----" und ab Punkt 2 dann wieder als normale Linie.

borose
21-04-2011, 09:05
Hi,

ich denke, das geht leider nicht direkt. 'xmin'/'xmax' kann man leider nicht für jeden Plot einzeln festlegen. Eine Beschränkung auf die einzulesenen Zeilen weiß ich auch nicht.

Wenn du aber Hand anlegst, gehts auf jeden Fall. Will heißen. Erstelle 2 unterschiedliche Datendateien aus deiner Ausgangsdatei. Die eine für den ersten Datenbereich und die zweite für den anderen Datenbereich. Du musst jeweils die nicht benötigten Datensätze löscht oder per '%' bzw '#' ausklammern.
Dann mit jeweils angepeassten addplot-Befehl die 2 Files einbinden.

Schade, dass so ein Feature nicht eingebaut ist. Bzw. hab ich es nur übersehen?


Viele Grüße