我想使用 创建两个函数的图pgfplots
。这两个图相交。其中一个图的形状应从相交处开始从实线变为虚线。
下面,我创建了一个简化的示例来说明我想要实现的功能。在该示例中,我手动计算了交点来获取域。我怎样才能自动计算正确的域?
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}
% First plot
\addplot [name path=f,domain={0:5}] {x};
% Second plot
\addplot [dashed,name path=g,domain={0:5}] {1 + 0.5*x};
% Get the intersection of plots and mark it
\draw [name intersections={of=f and g,name=i}] (i-1) circle (2pt);
% Do something useful to calculate the domain
\addplot [domain={0:2}] {1+0.5*x};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
一种可能更简单、更通用的方法是使用intersection segments
pgfplots 库提供的fillbetween
。虽然该库的主要应用是启用线段之间的填充,但它也可用于以不同的样式绘制不同的线段。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}
% First plot
\addplot [name path=f,domain={0:5}] {x};
% Second plot
\addplot [draw=none,name path=g,domain={0:5}] {1 + 0.5*x};
% Get the intersection of plots and mark it
\draw [name intersections={of=f and g,name=i}] (i-1) circle[radius=2pt];
% Do something useful to calculate the domain
\draw[ intersection segments={
of= f and g,
sequence={R1},
}];
\draw[dashed,intersection segments={
of= f and g,
sequence={R2},
}];
% \addplot [domain={0:2}] {1+0.5*x};
\end{axis}
\end{tikzpicture}
\end{document}