2

I have a couple of curves I am drawing in an axis, but one of them is a circle that is drawn with a "fancy" line (I just overlay a thick black curve with a slightly thinner white dashed curve). Is there a way I can fake a legend entry to match?

I have looked in the pgfplots manual and I see \addlegendimage and \addlegendentry but can't think of how to use it except for a rudimentary simple line:

\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.5xx} );

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

enter image description here

How can I make the circle legend have the same line pattern?

Stefan Pinnow
  • 29,535
Jason S
  • 2,838
  • Yoou should be able to just use the same style. But in you case you draw two separate plots to obtain the fancy style. If you come up with a single style that daws you graph, then it should work. – Peter Grill Sep 15 '20 at 04:05
  • Yeah, sure, but I don't know how to do that with a single style. I like the bent-striped-soda-straw look much better than a single line. There's no way to draw two different legend lines in the same spot? (without having to recreate the function of pgfplots legend) – Jason S Sep 15 '20 at 04:08
  • One way would be to use a decoration on the path, but perhaps there is an easier way. – Peter Grill Sep 15 '20 at 04:18

1 Answers1

4

Using option postaction (or preaction), the two \draws can be merged to one. These two options are documented in pgfmanual, sec. 15.10 Doing Multiple Actions on a Path in v3.1.5b.

Similar to this answer, an example using postaction is (note the option draw used in 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}

enter image description here

By wrapping all the options in a new style double colors, your example can be drawn by (note the dash phase=-4pt added to \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.5xx} );

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

enter image description here

muzimuzhi Z
  • 26,474
  • Wow! I saved this. Thanks! – Rmano Sep 15 '20 at 09:53
  • Thanks! So I guess the takeaway here is that it is difficult (impossible?) to draw custom art in a legend; the cleaner approach in pgfplots is to encapsulate it as a style that can be applied to the line that the legend-handling logic can just use. – Jason S Sep 15 '20 at 18:15
  • @JasonS Ah I just find the option legend image code/.code={...}, which can be used to set arbitrary drawing code for all legends. By default this option is set by legend line to \draw plot coordinates {...};. Not sure if there is an easy way to only change drawing code for specific legends. – muzimuzhi Z Sep 15 '20 at 20:55