3

Is it possible to customize the style of legend keys (the part of legend which indicates the line type being solid, dashed, dotted etc.)?

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[domain=0:10, no marks]
        \addplot+[red, solid] {x - 1}; \label{one}
        \addplot+[blue, solid] {2*x - 3}; \label{two}
        \addlegendentry{$m = 2$} 
        \addlegendentry{$m = 1$}
        \addplot+[red, densely dotted] {x - 0}; \label{three}
        \addplot+[blue, densely dotted] {2*x + 2}; \label{four}

        % Rough look of desired legend
        \draw[densely dotted, blue] (0, 11) -- (0.5, 11);
        \draw[solid, blue] (0.5, 11) -- node[xshift=0.1cm, right] {$m = 1$} (1, 11);
        \draw[densely dotted, red] (0, 13) -- (0.5, 13);
        \draw[solid, red] (0.5, 13) -- node[xshift=0.1cm, right] {$m = 2$} (1, 13);
\end{axis}
\end{tikzpicture}
\end{document}

Instead of having a legend for each curve, I would like two legend entries which indicate the line slope $m$ for both solid and dashed lines. So, the legend entry $m = 2$ will be referenced by a red line segment, half of which is dashed and the other half solid. Something like \addlegendimage{custommarker, red}, perhaps. Is there a process in place for doing this?

This question is different in that I am interesting in creating a non-default legend key. The whole legend could be created from scratch, but wondering if there is an inbuilt way to do this (if default dimensions of legend boxes, white spaces etc could be easily extracted, perhaps from scratch would be simpler).

1 Answer 1

4

You only need to set up your own legend image code for that.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{/pgfplots/half and half legend/.style={
        /pgfplots/legend image code/.code={%
            \draw[##1,/tikz/.cd,densely dotted]
            (0cm,0cm) -- (0.3cm,0cm);
            \draw[##1,/tikz/.cd,solid]
            (0.3cm,0cm) -- (0.6cm,0cm);},},              
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[domain=0:10, no marks,half and half legend]
        \addplot+[red, solid] {x - 1}; \label{one}
        \addplot+[blue, solid] {2*x - 3}; \label{two}
        \addlegendentry[red]{$m = 2$} 
        \addlegendentry[blue]{$m = 1$}
        \addplot+[red, densely dotted] {x - 0}; \label{three}
        \addplot+[blue, densely dotted] {2*x + 2}; \label{four}

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

2
  • Would you mind explaining what ##1 means? I understand that it allows something to become a parameter after the initial macro is expanded, but what becomes the parameter in this case?
    – mcmuffin6o
    Commented Feb 22 at 1:07
  • I would think that it allows the options passed to addplot+ to pass through to the legend image, but removing the ##1 from the \draw call doesn't seem to change anything...
    – mcmuffin6o
    Commented Feb 22 at 1:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .