I am trying to draw a graph with some legends. I have two questions:
If I use
\legend, then it has to give legends for all the plots above.If I use
\addlegendentry, it cannot give correct graph (the picture below).
How can I do it?
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[clip=false, xlabel=$1-\lambda$,ylabel={$\beta$}, ytick=\empty,xtick=\empty,
x label style={at={(1.05,0)},anchor=south},
y label style={at={(0.091,1)},anchor=north,rotate=270},
axis line style={draw=none}, legend style={at={(1.4,1.1)},legend cell align=right,legend plot pos=right}, area legend
]% coordinates
\addplot [domain=0:1, no marks, pattern=north west lines, area legend] {0.5} \closedcycle;\addlegendentry{$\beta \leq \frac{1}{2}$}
\addplot [name path=A,domain=0:1,black] {0.5};
\addplot [name path=B,domain=0:1,black,samples=300] {0.5+0.5*sqrt(1-x)};
\addplot [name path=C,domain=0:1,black,samples=300] {0.5*(2-x+sqrt(2*(1-x)+2*(1-x)^2))};
\addplot [pattern=dots, pattern color=green, draw=none,area legend] fill between [of=A and B];
\addlegendentry {$\frac{1}{2}<\beta \leq \frac{1}{2}(1+\sqrt{\lambda})$}
\addplot [
fill=blue, fill opacity=0.3,
draw=none, area legend
] fill between [of=B and C];\addlegendentry {$\frac{1}{2}(1+\sqrt{\lambda})<\beta<\frac{1}{2}(1+\lambda+\sqrt{2}\sqrt{\lambda+\lambda^2})$}
\addplot [ no marks] coordinates {(0,0) (0,2.5)} node[left,pos=1] {$+\infty$};
\addplot [ no marks] coordinates {(0,0) (1,0)} node[below, pos=1] {$1$};
\addplot [ no marks] coordinates {(1,0) (1,2.5)};
\addplot [ no marks] coordinates {(0,2.5) (1,2.5)};
\node [draw=none] at (rel axis cs: 0.04,0.04) {0};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

forget plot. Add this key to all\addplotcommands except for these for which you want a legend. – Dec 17 '19 at 18:38