Wie kann ich mit pgfplots eine schicke „Proxy“-Legendenlinie zeichnen?

Wie kann ich mit pgfplots eine schicke „Proxy“-Legendenlinie zeichnen?

Ich zeichne ein paar Kurven auf einer Achse, aber eine davon ist ein Kreis, der mit einer „ausgefallenen“ Linie gezeichnet ist (ich überlagere einfach eine dicke schwarze Kurve mit einer etwas dünneren weißen gestrichelten Kurve). Gibt es eine Möglichkeit, einen Legendeneintrag zu fälschen, der dazu passt?

Ich habe im Handbuch zu pgfplots nachgesehen und sehe \addlegendimageund, \addlegendentrykann mir aber keine Verwendung vorstellen, außer einer rudimentären einfachen Zeile:

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

Bildbeschreibung hier eingeben

Wie kann ich dafür sorgen, dass die Kreislegende das gleiche Linienmuster aufweist?

Antwort1

Mit der Option postaction(oder preaction) \drawkönnen die beiden s zu einem zusammengeführt werden. Diese beiden Optionen sind in pgfmanual, Abschnitt 15.10 dokumentiert.Mehrere Aktionen auf einem Pfad ausführenin v3.1.5b.

Ähnlich zudiese Antwort, ein Beispiel für die Verwendung postactionist (beachten Sie die drawin verwendete Option 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}

Bildbeschreibung hier eingeben

Indem Sie alle Optionen in einen neuen Stil einschließen double colors, kann Ihr Beispiel gezeichnet werden durch (beachten Sie das dash phase=-4ptHinzugefügte zu \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}

Bildbeschreibung hier eingeben

verwandte Informationen