
私は新規ユーザーですPGFPlotsそこで、次の質問があります。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 軸環境で凡例の位置をカスタマイズするしかし、これを axis 環境に配置する方法がわかりません。
答え1
\addlegendimage
コマンドの凡例を追加するには を使用できます\draw
。 の引数は\addlegendimage
で使用されるのと同じスタイル パラメータですdraw
(例: ) red,line width=0.3mm
。ただし、このように を使用する場合の問題\draw
は、 の座標系を使用しないため、スケールが間違っていることですaxis
。
addplot
ここでのもう 1 つのオプションは、正しいサイズを与える楕円を でプロットすることです。
\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}