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.