0

I am trying to draw a graph with some legends. I have two questions:

  1. If I use \legend, then it has to give legends for all the plots above.

  2. 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}

enter image description here

Tonechas
  • 976
  • As you say, use forget plot. Add this key to all \addplot commands except for these for which you want a legend. –  Dec 17 '19 at 18:38
  • I guess I know the answer of the first question, just using "forget plot"... But I still do not understand the second question. – latexishardwowowo Dec 17 '19 at 17:46

0 Answers0