0

I have a problem with a legend of filled area between curves. I want to have a legend item with a small square which represent a filled with fill between function. When I used "area legend" in /addplot, the legend item showed just like dashed line not as a small square.

\begin{figure}[H]
\centering
\begin{tikzpicture}[scale = 0.91]
\begin{axis}[
    scale only axis,
    xlabel={Napätie \(U_{DS} [V]\)},
    ylabel={Prúd \(I_D [A]\)},
    xmin=0, xmax=20,
    ymin=0, ymax=0.8,
    ymajorgrids=true,
    xmajorgrids=true,
    legend style={at={(0.5,0.98)},anchor=north}
]
\addplot[line width=1.5pt,solid,color=blue] 
coordinates {(0,0)(1.67,0.16)(1.8,0.18)(2.2,0.2)(17.7,0.22)(18.5,0.25)(19,0.3)(19.8,0.8)};
\addlegendentry{\(U_{GS}\) = 2 V}

\addplot[line width=1.5pt,solid,color=red] 
coordinates {(0,0)(2.2,0.34)(2.35,0.37)(2.9,0.4)(18,0.42)(18.2,0.43)(18.7,0.445)(19,0.48)(19.7,0.8)};
\addlegendentry{\(U_{GS}\) = 2.2 V}

\addplot[line width=1.5pt,solid,color=green] 
coordinates {(0,0)(2.6,0.56)(2.8,0.58)(3.2,0.6)(18,0.62)(18.4,0.64)(18.6,0.65)(19,0.7)(19.6,0.8)};
\addlegendentry{\(U_{GS}\) = 2.4 V}

\addplot[name path=B, line width=1pt,solid,color=black, smooth,dashed] 
coordinates {(0,0)(2.2,0.2)(2.9,0.4)(3.2,0.6)(3.5,0.8)};

\addplot[name path=C, line width=1pt,solid,color=black, smooth,dashed] 
coordinates {(16.5,0)(17.7,0.22)(18,0.42)(18,0.62)(18.2,0.8)};

\addplot[draw=none, name path=A, line width=1pt,solid] 
coordinates {(0,0)(0.01,0.8)};

\addplot[draw=none, name path=D, line width=1pt,solid] 
coordinates {(19.9,0)(20,0.8)};

\addplot[pattern=north west lines, pattern color=apricot, area legend] fill between[of=A and B];
\addlegendentry{Saturation region}
\addplot[pattern=north west lines, pattern color=babyblueeyes] fill between[of=B and C];
\addplot[pattern=north west lines, pattern color=babypink] fill between[of=C and D];

\end{axis}
\end{tikzpicture}
\caption{Výstupná charakteristika MOSFET tranzistora}
\label{fig:MOS_output}

\end{figure}

3

You forgot a couple of forget plots (and also to provide an MWE, so I had to replace some colors). forget plot can be used to disregard the respective plot from the legend.

\documentclass[tikz,border=3mm,dvipsnames]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.17} 
\begin{document}
\begin{tikzpicture}[scale = 0.91]
\begin{axis}[
    scale only axis,
    xlabel={Nap\"atie \(U_{DS} [V]\)},
    ylabel={Pr\'ud \(I_D [A]\)},
    xmin=0, xmax=20,
    ymin=0, ymax=0.8,
    ymajorgrids=true,
    xmajorgrids=true,
    legend style={at={(0.5,0.98)},anchor=north}
]
\addplot[line width=1.5pt,solid,color=blue] 
coordinates {(0,0)(1.67,0.16)(1.8,0.18)(2.2,0.2)(17.7,0.22)(18.5,0.25)(19,0.3)(19.8,0.8)};
\addlegendentry{\(U_{GS}\) = 2 V}

\addplot[line width=1.5pt,solid,color=red] 
coordinates {(0,0)(2.2,0.34)(2.35,0.37)(2.9,0.4)(18,0.42)(18.2,0.43)(18.7,0.445)(19,0.48)(19.7,0.8)};
\addlegendentry{\(U_{GS}\) = 2.2 V}

\addplot[line width=1.5pt,solid,color=green] 
coordinates {(0,0)(2.6,0.56)(2.8,0.58)(3.2,0.6)(18,0.62)(18.4,0.64)(18.6,0.65)(19,0.7)(19.6,0.8)};
\addlegendentry{\(U_{GS}\) = 2.4 V}

\addplot[name path=B, line width=1pt,solid,color=black, smooth,dashed,forget plot] 
coordinates {(0,0)(2.2,0.2)(2.9,0.4)(3.2,0.6)(3.5,0.8)};

\addplot[name path=C, line width=1pt,solid,color=black, smooth,dashed,forget plot] 
coordinates {(16.5,0)(17.7,0.22)(18,0.42)(18,0.62)(18.2,0.8)};

\addplot[draw=none, name path=A, line width=1pt,solid,forget plot] 
coordinates {(0,0)(0.01,0.8)};

\addplot[draw=none, name path=D, line width=1pt,solid,forget plot] 
coordinates {(19.9,0)(20,0.8)};

\addplot[pattern=north west lines, pattern color=Apricot, area legend] fill between[of=A and B];
\addlegendentry{Saturation region}
\addplot[pattern=north west lines, pattern color=blue!20] fill between[of=B and C];
\addplot[pattern=north west lines, pattern color=pink] fill between[of=C and D];

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.