区域图例之间的填充

区域图例之间的填充

我对曲线之间填充区域的图例有疑问。我想要一个带有小方块的图例项,它表示函数之间的填充。当我在 /addplot 中使用“区域图例”时,图例项显示为虚线而不是小方块。

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

\结束{图}

答案1

你忘了几个forget plots(并且还要提供 MWE,所以我不得不替换一些颜色)。forget plot可以用来忽略图例中的相应情节。

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

在此处输入图片描述

相关内容