
Ich bin ein neuer Benutzer vonPGFlotsalso ich habe folgende Frage: Ich zeichne eine Ellipse in TikZ und möchte eine Legende dafür hinzufügen. Ich habe jedoch festgestellt, dass dies \addlegendentry
nur für gilt \addplot
. Wie kann ich dann eine Legende für meine Kurve hinzufügen?
\tikzsetnextfilename{1a}
\begin{tikzpicture}
\begin{axis}[
xlabel = $\dfrac{M}{M_{bend}}$,
ylabel = $\dfrac{P}{P_{burst}}$,
xmin=0,xmax=1.2,
ymin=0,ymax=1.2,
ymajorgrids=true,
xmajorgrids=true,
grid style=dashed,
label style={font=\tiny},
tick label style={font=\tiny},
legend style={font=\small},
legend cell align=left,
legend pos=north east
]
\addplot+[color=red,
line width=0.3mm]
(axis cs:0,0)
ellipse [
x radius=0.950052, y radius=0.945021];
\addlegendentry{Analytical results}
\end{axis}
\end{tikzpicture}
Ich habe über die angepasste Legende gelesen von Passen Sie die Legendenposition in der Achsenumgebung von PGFplots an, aber ich habe keine Ahnung, wie ich das in eine Achsenumgebung einfügen kann.
Antwort1
Sie können verwenden, \addlegendimage
um eine Legende für einen Befehl hinzuzufügen \draw
. Das Argument für \addlegendimage
ist genau derselbe Stilparameter, der in verwendet wird draw
, z. B. red,line width=0.3mm
. Das Problem bei dieser Verwendung \draw
ist jedoch, dass der Maßstab falsch ist, da nicht das Koordinatensystem von verwendet wird axis
.
Eine weitere Möglichkeit besteht darin, die Ellipse mit zu zeichnen addplot
, wodurch die richtige Größe erhalten wird.
\documentclass[tikz,border=5mm]{standalone}
\usepackage{pgfplots,amsmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel = $\dfrac{M}{M_{\mathrm{bend}}}$,
ylabel = $\dfrac{P}{P_{\mathrm{burst}}}$,
xmin=0,xmax=1.2,
ymin=0,ymax=1.2,
ymajorgrids=true,
xmajorgrids=true,
grid style=dashed,
label style={font=\tiny},
tick label style={font=\tiny},
legend style={font=\small},
legend cell align=left,
legend pos=north east
]
\draw[color=red,
line width=0.3mm]
(axis cs:0.6,0.6) % moved to middle of plot to make it more visible
ellipse [
x radius=0.950052, y radius=0.945021];
\addlegendimage{line width=0.3mm,color=red}
\addlegendentry{Analytical results}
\addplot [variable=\t,samples=200,domain=0:360] ({0.950052*cos(t)},{0.945021*sin(t)});
\addlegendentry{Parametric analytic}
\end{axis}
\end{tikzpicture}
\end{document}