# How to increase legend lines lengths in PGFplots

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

\begin{document}

\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}

\end{document}


• When you say " I want to keep the marks distances as they are in the actual plot " modifying coordinate of marks inside legend does not affect the distance between marks in the main plots. Aug 13, 2015 at 15:49
• @salimbou: No it does not. But that is not my problem. My problem is that if I use that solution the distances between the marks in the legend wouldn't be the same as the ones in the plot. Aug 13, 2015 at 15:56

A sample:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
/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}
\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.

• So if I get it right. The legend itself is a manual plot. If I want to have the same marks distances in the legend and in the plot I have to do it manually. For example, in your example the distance between the red circles in the legend is more than the distance between the red circles in the plot. If I want the same distance between marks in the legend and the plot, I have to manually manipulate the coordinate points and its NOT something that I can get automatically, so that the marks distances in the plot and in the legend sync automatically. Am I right? Aug 14, 2015 at 6:54
• @RozaTh Try removing mark repeat=2,mark phase=2, from the first example above. Aug 14, 2015 at 7:59
• @RozaTh I updated Aug 14, 2015 at 8:40
• Excellent answer @Symbol1!! I have a question: Do you know how to modify the length of just one legend line length? Suppose I want to make larger just the legend \addlegendentry{b}, the other must not be changed. Is that possible? Jul 19, 2020 at 1:32
• @manooooh Can you ask a separate question? I don't have anything right away to offer in comments. (Other than using local variables to test which legend we are drawing.) Jul 19, 2020 at 20:33