# pgfplot legend with fill, of fill between two curves

I would lie to make a legend where the legend marker is the fill of the area. However, when filling between two curves in pgfplots the lines are defined before the fill, so I have idea how to do this.

My current MWE:

\documentclass[border=0.5]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mathtools,amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
mark = none,
axis equal,
xmin = 0,
xmax = 2,
ymin = 0,
ymax = 2,
]
\addplot[pattern= dots, samples = 3, domain = 0:1, pattern color = red] {1}\closedcycle;
\addplot+[name path global = A, color = black, mark=none, domain = 1:2] {1};
\addplot+[name path global = B, color = black, mark=none, domain = 1:2] {2};
\addplot[pattern = north west lines, pattern color = green] fill between[of=A and B];
\draw [color = black] (axis cs:1,1) -- (axis cs:1,2);
\draw [color = black] (axis cs:2,1) -- (axis cs:2,2);
\filldraw [color = blue] (axis cs:1,1) circle (0.005);
\legend{ First electron, Other electron}
\end{axis}
\end{tikzpicture}
\end{document}


This produces:

Where I would like the black lines to be the pattern of the squares.

P.S.: I do realize that the current legend placement is horrible, but no point in moving it if I would rather explain myself in a caption ;-)

So you want do do something like the following?
For details please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{
patterns,
pgfplots.fillbetween,
}
\pgfplotsset{
compat=1.15,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
no markers,                 % <-- changed from mark=none'
axis equal,
xmin=0,
xmax=2,
ymin=0,
ymax=2,
]
pattern=dots,
samples=3,
domain=0:1,
pattern color=red,
area legend,            % <-- added to change the type of legend symbol
] {1}
\closedcycle
;

draw=none,              % <-- added, so it is invisible
forget plot,            % <-- added, so it doesn't count for the legend
name path global=A,
domain=1:2,
] {1};
draw=none,              % <-- added, so it is invisible
forget plot,            % <-- added, so it doesn't count for the legend
name path global=B,
domain=1:2,
] {2};
pattern=north west lines,
pattern color=green,
draw=black,             % <-- added to draw the frame
] fill between [of=A and B];

\filldraw [color = blue] (axis cs:1,1) circle (0.005);

\legend{
First electron,
Other electron
}
\end{axis}
\end{tikzpicture}
\end{document}
`

• This is actually way cooler than answers I have seen to similar questions (there they define a new type of legend and use that) really nice ;-) – Thorbjørn E. K. Christensen Feb 14 '18 at 19:56
• Could you state some of the found questions here so I may have a look? Maybe it is worth either writing an answer there too or at least adding a comment stating thar here is a similar question with an answer. – Stefan Pinnow Feb 14 '18 at 20:00
• It would seem that I read the previous questions wrong. They do define their own legend style, but they use patterns on a bar chart – Thorbjørn E. K. Christensen Feb 14 '18 at 20:05