带填充的 pgfplot 图例,两条曲线之间的填充

带填充的 pgfplot 图例,两条曲线之间的填充

我想制作一个图例,其中图例标记是区域的填充。但是,当在两条曲线之间填充时,pgfplots线条在填充之前定义,所以我知道如何做到这一点。

我的当前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}

得出的结果为: 在此处输入图片描述

我希望黑线成为方块的图案。

提前致谢 ;-)

附言:我确实意识到当前图例的位置很糟糕,但如果我宁愿在标题中解释自己,那么移动它就没有意义了;-)

答案1

那么你想做如下的事情吗?
有关详细信息,请查看代码中的注释。

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

该图显示了上述代码的结果

相关内容