[Tex/LaTex] pgfplot legend with fill, of fill between two curves

fillbetweenlegendpgfplotstikz-pgf

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:
enter image description here

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

Thanks in advance 😉

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 😉

Best Answer

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,
    ]
        \addplot [
            pattern=dots,
            samples=3,
            domain=0:1,
            pattern color=red,
            area legend,            % <-- added to change the type of legend symbol
        ] {1}
            \closedcycle
        ;

        \addplot [
            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};
        \addplot [
            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};
        \addplot [
            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}

image showing the result of above code

Related Question