
我是新用戶PGFP圖所以我有以下問題:我在 TikZ 中畫了一個橢圓,我想為其添加一個圖例。但是,我發現\addlegendentry
僅適用於\addplot
.那麼,如何為我的曲線添加圖例呢?
\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}
我讀到了關於定製圖例的信息 在 PGFplots 軸環境中自訂圖例位置,但我不知道如何將其放入軸環境中。
答案1
您可以使用\addlegendimage
為命令新增圖例\draw
。的參數\addlegendimage
與 中使用的樣式參數相同draw
,例如red,line width=0.3mm
。然而,像這樣使用的問題\draw
是比例是錯誤的,因為它不使用axis
.
這裡的另一個選擇是用 繪製橢圓addplot
,這給出了正確的大小。
\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}