如何使用 pgfplots 绘制精美的“代理”图例线?

如何使用 pgfplots 绘制精美的“代理”图例线?

我在轴上绘制了几条曲线,但其中一条是用“花式”线条绘制的圆(我只是在一条粗黑曲线上叠加了一条稍细的白色虚线)。有没有办法伪造一个图例条目来匹配?

我查看了 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),可以将两个\draws 合并为一个。这两个选项记录在 的pgfmanual15.10 节中在路径上执行多个操作在 v3.1.5b 中。

如同这个答案,使用的示例是(请注意中使用的postaction选项)drawpostactions={...}

\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}

在此处输入图片描述

相关内容