[Tex/LaTex] How to increase legend lines lengths in PGFplots

legendpgfplotstikz-pgf

I want to know how I can make PGFplots to draw longer segments in the legend. I looked at couple of previously asked questions and none seemed to work for me.

In this question: "adjust the legend line length" the given solution is to manually draw the image lines and specify the coordinates of the marks. I don't want this since I want to keep the marks distances in the legend as they are in the actual plot.

The solution to this question: adjust the column width of legend , stretches the lines and therefore stretches the marks instead of adding more (hence only works with solid lines too).

I took the below example from the second question. What I want is to naturally increase the lengths of the lines in the legend so that the distance between marks, and dots and dashes are the same as the plot itself, just a longer segment.

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{
     compat=newest
}    

\begin{document}

\begin{tikzpicture}
\begin{axis}
    \addplot+[dashdotted,mark=triangle] plot {x^2};
    \addlegendentry{a}
    \addplot+[dotted,mark=*] plot {1};
    \addlegendentry{b}
    \addplot+[mark=star] plot {x};
    \addlegendentry{c}
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Best Answer

A sample:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
    compat=newest,
    /pgfplots/legend image code/.code={%
        \draw[mark repeat=2,mark phase=2,#1] 
            plot coordinates {
                (0cm,0cm) 
                (0.3cm,0cm)
                (0.6cm,0cm)
                (0.9cm,0cm)
                (1.2cm,0cm)%
            };
    },
}
\begin{tikzpicture}
    \begin{axis}
        \addplot+[dashdotted,mark=triangle] plot {x^2};
        \addlegendentry{a}
        \addplot+[dotted,mark=*] plot {1};
        \addlegendentry{b}
        \addplot+[mark=star] plot {x};
        \addlegendentry{c}
    \end{axis}
\end{tikzpicture}
\end{document}

Another sample

    plot coordinates {
        (0cm,0cm) 
        (0.3cm,.1cm)
        (0.6cm,0cm)
        (0.9cm,-.1cm)
        (1.2cm,0cm)%
    };

Explanation

Go to pgfplots.code.tex and find this:

/pgfplots/line legend/.style={%
  /pgfplots/legend image code/.code={%
      \draw[mark repeat=2,mark phase=2,##1] 
          plot coordinates {
              (0cm,0cm) 
              (0.3cm,0cm)
              (0.6cm,0cm)%
          };%
  }%
},    
/pgfplots/line legend/.style/.code={\pgfplots@error{This style is supposed to be constant.}},%
/pgfplots/line legend/.append style/.code={\pgfplots@error{This style is supposed to be constant.}},%

This shows us that

  • A legend entry is indeed a plot;
  • that plot consists of three fixed point, the second marked; and last, but the worst
  • you cannot modify this style anymore because /.style/.code make it meaningless to say /.style={new style}. (It throw the error and ignore your suggestion)

So... in general, one solution is to copy pgfplots.code.tex to your current folder and modify those lines to, say,

    /pgfplots/line legend/.style={%
        /pgfplots/legend image code/.code={%
            \draw[mark repeat=2,mark phase=2,##1] 
                plot coordinates {
                    (0cm,0cm) 
                    (0.3cm,0cm)
                    (0.6cm,0cm)
                    (0.9cm,0cm) 
                    (1.2cm,0cm)%
                };%
        }%
    },  

But at the beginning of my answer I need no new pgfplots.code.tex because we do not really care about /pgfplots/line legend/.style and we can simply manipulate /pgfplots/legend image code/.code. However, one obvious drawback is that it nullifies any previous /pgfplots/legend image code/.add code, or .append code or .prefix code.

About distance of marks

In your case, the default samples=25 and domain=-5:5 are used. So there is one mark every .4 unit in x direction. Therefore we expect the following assignment gives the correct result.

        plot coordinates {
            (axis cs:.0,-5)
            (axis cs:.2,-5)
            (axis cs:.4,-5)
            (axis cs:.6,-5)
            (axis cs:.8,-5)
        };

In general it is quite hard to tell the actual (horizontal) distance between marks since PgfPlots does scaling quite often. By general I meant that you may have data points with periodic x-values but periods vary from line to line. Manual calculation is doable only if you are plotting a function. But then (a) you do not need PgfPlots but TikZ and (b) it is meaningless to add marks which represent data.