Seite 1 von 1

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

Verfasst: Sa 13. Dez 2014, 12:57
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

Verfasst: So 14. Dez 2014, 20:11
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

Verfasst: Fr 19. Dez 2014, 17:44
von Flo Rida
Das funktioniert super! Danke.