The option legend image post style
is precisely what you need:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\pgfplotscreateplotcyclelist{mycyclelist}{
{loosely dashed, blue}, {red}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
cycle list name = mycyclelist,
legend entries={a, b},
legend pos=north west,
legend image post style={sharp plot},
]
% only marks
\addplot+[only marks]
coordinates
{
(0,0)
(10,5)
};
\addplot+[only marks]
coordinates
{
(0,0)
(10,7)
};
% no markers
\addplot+[no markers]
coordinates
{
(0,0)
(10,5)
};
\addplot+[no markers]
coordinates
{
(0,0)
(10,7)
};
\end{axis}
\end{tikzpicture}
\end{document}
The list of options after legend image post style
applies to every legend image and is independent of the plot style. Adding sharp plot
essentially overrides the only marks
statement - but only for legend images.
forget plot
. This key leaves the \addplot
in question out of the legend.
\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{patterns,arrows}
\begin{document}
\definecolor{plotrangecolor}{HTML}{D8D8D8} %{D8D8D8}
%%
%% http://tex.stackexchange.com/questions/29359/pgfplots-how-to-fill-the-area-under-a-curve-with-oblique-lines-hatching-as-a
%%
\tikzset{
hatch distance/.store in=\hatchdistance,
hatch distance=10pt,
hatch thickness/.store in=\hatchthickness,
hatch thickness=2pt
}
\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
\pgfsetcolor{\tikz@pattern@color}
\pgfsetlinewidth{\hatchthickness}
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
\pgfusepath{stroke}
}
\begin{tikzpicture}
\begin{axis}[
xmin=1, xmax=15,
domain=1:15,
ymin=0, ymax=1.1,
no markers,
enlargelimits=false,
xlabel={x},
ylabel={y},
legend style={
cells={anchor=west},
legend pos=south east
}
]
\addplot[mark=none,
domain=2:4,
pattern=flexible hatch,
hatch distance=10pt,
hatch thickness=0.5pt,
draw=none,
pattern color=plotrangecolor,
area legend,
forget plot] {2} \closedcycle;
\addplot[mark=none,
domain=8:14,
pattern=flexible hatch,
hatch distance=10pt,
hatch thickness=0.5pt,
draw=none,
pattern color=plotrangecolor,
area legend,
forget plot] {2} \closedcycle;
\addplot coordinates {(1,1) (4,0.4) (7,0.6) (8,0.5) (14,0.2)};
\addplot coordinates {(1,0.2) (3,0.3) (5,0.7) (9,1) (14,0.5)};
\addlegendentry{value 1}
\addlegendentry{value 2}
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
A sample:
Another sample
Explanation
Go to
pgfplots.code.tex
and find this:This shows us that
/.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,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
anddomain=-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.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.