Legende rechts außerhalb, bündig über rechtem y-Label

Tabellen und Grafiken erstellen und anordnen


Flo Rida
Forum-Fortgeschrittener
Forum-Fortgeschrittener
Beiträge: 71
Registriert: Do 22. Mai 2014, 23:07

Legende rechts außerhalb, bündig über rechtem y-Label

Beitrag von Flo Rida »

Hallo,

ich habe in meinen Grafiken meistens zwei Ordinaten. Die Legende soll stets außerhalb angeordnet sein. Jetzt ist es leider wie in meinem Beispiel, dass ich entweder mit meiner Legende "auf" den yticks liege, oder wenn ich die Legende manuell weit außerhalb anordne unschöne Abstände zu meinen yticks habe. Ich möchte, am liebsten, dass die Legende sich wie das rechte y-label je nach Länge der Zahl der yticks verschiebt. Hier mein beispiel, dann seht ihr was ich meine.
\documentclass[a4paper]{scrartcl}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[legend pos=outer north east,ytick pos=right,ylabel={ein Label}]
\addplot coordinates{
(10,10)
(100,100)
(1000,1000)
(2000,5000)
};
\addlegendentry={ABC}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[legend pos=outer north east,ytick pos=right,ylabel={ein Label}]
\addplot coordinates{
(-2,-2)
(0,0)
(3,4.6)
};
\addlegendentry={ABC}
\end{axis}
\end{tikzpicture}

\pgfplotsset{every axis legend/.append style={at={(1.2,1)},anchor=north west}}

\begin{tikzpicture}
\begin{axis}[ytick pos=right,ylabel={ein Label}]
\addplot coordinates{
(10,10)
(100,100)
(1000,1000)
(2000,5000)
};
\addlegendentry={ABC}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[ytick pos=right,ylabel={ein Label}]
\addplot coordinates{
(-2,-2)
(0,0)
(3,4.6)
};
\addlegendentry={ABC}
\end{axis}
\end{tikzpicture}

\end{document}
Viele Grüße
Flo Rida

esdd
Forum-Meister
Forum-Meister
Beiträge: 2561
Registriert: So 7. Feb 2010, 16:36

Beitrag von esdd »

Du kannst das yticklabel cs für die Positionierung nutzen:
\documentclass[margin=10pt]{standalone} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.11} 

\begin{document} 

\pgfplotsset{legend style={at={(yticklabel cs:1)},anchor=north west}} 

\begin{tikzpicture} 
\begin{axis}[ytick pos=right,ylabel={ein Label}] 
\addplot coordinates{ 
 (10,10) 
 (100,100) 
 (1000,1000) 
 (2000,5000) 
}; 
\addlegendentry={ABC} 
\end{axis} 
\end{tikzpicture} 

\begin{tikzpicture} 
\begin{axis}[ytick pos=right,ylabel={ein Label}] 
\addplot coordinates{ 
 (-2,-2) 
 (0,0) 
 (3,4.6) 
}; 
\addlegendentry={ABC} 
\end{axis} 
\end{tikzpicture} 

\end{document}
Gruß
Elke

Flo Rida
Forum-Fortgeschrittener
Forum-Fortgeschrittener
Beiträge: 71
Registriert: Do 22. Mai 2014, 23:07

Beitrag von Flo Rida »

Das funktioniert super! Danke.

Antworten