軸に描く曲線がいくつかありますが、そのうちの 1 つは「派手な」線で描かれた円です (太い黒の曲線に、少し細い白い破線曲線を重ねただけです)。凡例エントリを偽装して一致させる方法はありますか?
pgfplots のマニュアルを見てみましたが\addlegendimage
、\addlegendentry
基本的な単純な行以外の使用方法がわかりません。
\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{sansmath}
\pgfplotsset{compat=1.17}
% pgfplots package manual at https://ctan.org/pkg/pgfplots?lang=en
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis equal,
width=10cm, height=6cm,
font=\sffamily,
ticklabel style = {font=\sansmath\sffamily},
xmin=-0.7,xmax=0, xlabel={$x$}, xtick={-1,-0.9,...,0}, minor xtick={-1,-0.95,...,0},
ymin=0.7,ymax=1.0, ylabel={$y$}, ytick={0, 0.1,...,1}, minor ytick={0, 0.05,...,1},
samples=500,domain=-1:0,
grid=both,
legend pos = south east,
legend cell align = left,
title={\large shapes on axis}]
% I want to add a legend saying "circle"
\draw [black, line width = 0.7mm] (0,0) circle [radius=1.0];
\draw [white, line width = 0.5mm, dash pattern = on 5pt off 5pt] (0,0) circle [radius=1.0];
\addlegendimage{black, line width=0.7mm};
\addplot[blue, line width = 0.3mm]({x}, {1-0.5*x*x} );
% I want to add a legend saying "line"
\draw [green!50!black, line width=0.3mm] (-0.6,1) -- (-0.4,0.95);
\addlegendimage{green!50!black, line width=0.3mm};
\legend {circle, quadratic curve, line};
\end{axis}
\end{tikzpicture}
\end{document}
円の凡例に同じ線のパターンを持たせるにはどうすればよいですか?
答え1
オプションpostaction
(またはpreaction
) を使用すると、2 つの\draw
を 1 つに結合できます。これらの 2 つのオプションについてはpgfmanual
、 のセクション 15.10で説明されています。パス上で複数のアクションを実行するv3.1.5b では。
に似ているこの答えを使用した例は次のとおりです(で使用されているpostaction
オプションに注意してください)draw
postactions={...}
\documentclass[tikz, border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw
[postaction={draw, white, dash pattern=on 4pt off 4pt, dash phase=4pt, thick}]
[black, ultra thick]
(0,0) rectangle (3,2);
\end{tikzpicture}
\end{document}
すべてのオプションを新しいスタイルで囲むとdouble colors
、例は次のように描画されます(dash phase=-4pt
に追加された に注意してください\addlegendimage[...]
)。
\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{sansmath}
\pgfplotsset{compat=1.17}
\tikzset{
double colors/.style={
postaction={draw, white, line width = 0.5mm, dash pattern = on 5pt off 5pt},
black, line width = 0.7mm
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis equal,
width=10cm, height=6cm,
font=\sffamily,
ticklabel style = {font=\sansmath\sffamily},
xmin=-0.7,xmax=0, xlabel={$x$}, xtick={-1,-0.9,...,0}, minor xtick={-1,-0.95,...,0},
ymin=0.7,ymax=1.0, ylabel={$y$}, ytick={0, 0.1,...,1}, minor ytick={0, 0.05,...,1},
samples=500,domain=-1:0,
grid=both,
legend pos = south east,
legend cell align = left,
title={\large shapes on axis}]
% I want to add a legend saying "circle"
\draw [double colors] (0,0) circle [radius=1.0];
\addlegendimage{double colors, dash phase=-4pt, line width=0.7mm};
\addplot[blue, line width = 0.3mm]({x}, {1-0.5*x*x} );
% I want to add a legend saying "line"
\draw [green!50!black, line width=0.3mm] (-0.6,1) -- (-0.4,0.95);
\addlegendimage{green!50!black, line width=0.3mm};
\legend {circle, quadratic curve, line};
\end{axis}
\end{tikzpicture}
\end{document}