我有以下乳胶代码:
\begin{tikzpicture}
\begin{axis}[
xlabel=Strike Price (K),
ylabel={Put Value (p)}
]
\addplot [only marks=*,blue] plot coordinates {
(40,10)
(50,20)
(70,30)
};
\addplot [smooth=*,red] plot coordinates {
(40,10)
(70,30)
};
\end{axis}
\end{tikzpicture}
我想用任意凸函数连接两个端点,然后遮蔽该函数和当前线之间的区域,但不太清楚如何做到这一点。
答案1
您可以使用fillbetween
库来填充该区域。为了使其中一条曲线弯曲,我使用了smooth
选项。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=Strike Price (K),
ylabel={Put Value (p)}
]
\addplot [mark=*,blue,smooth,name path=A] plot coordinates {
(40,10)
(50,20)
(70,30)
};
\addplot [mark=o,red,name path=B] plot coordinates {
(40,10)
(70,30)
};
\addplot[olive!40] fill between[of=A and B];
\end{axis}
\end{tikzpicture}
\end{document}
或者可能是这样,带有注释的行
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=Strike Price (K),
ylabel={Put Value (p)}
]
%% the upper curve
\addplot [mark=*,blue,smooth,name path=A] plot coordinates {
(40,10)
(50,20)
(70,30)
};
%% The lower curve
\addplot [no marks,green,smooth,name path=B] plot coordinates {
(40,10)
(60,15)
(70,30)
};
%% the straight line
\addplot [mark=o,red,name path=C] plot coordinates {
(40,10)
(70,30)
};
%% filling
\addplot[olive!40] fill between[of=B and C];
\end{axis}
\end{tikzpicture}
\end{document}