在两个交点之间绘制一个函数与另外两个函数

在两个交点之间绘制一个函数与另外两个函数

我定义了三个函数,红色、蓝色和绿色。我想只在蓝色和绿色函数的交点之间绘制红色函数。最简单的方法是什么?

\documentclass{minimal}
\usepackage{tikz,pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot[red] coordinates {(0,0)(15,30)(30,33)(45,28)(60,10)};
\addplot[green] coordinates {(0,20)(30,0)(60,0)};
\addplot[blue] coordinates {(0,10)(30,10)(60,25)};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

上面的红色函数是简化的。

我也在寻找将红色函数的坐标声明为适当的函数,但我找不到方法。

答案1

当你在等待 pgfplots 答案时,这里有一个元帖子版本,展示了修剪路径的有用功能cutbeforecutafter

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
numeric u, v;
u = 5; v = 6;

path R, B, G;
R = ((0,0) -- (15,30) -- (30,33) -- (45,28) -- (60,10)) xscaled u yscaled v;
B = ((0,10) -- (30,10) -- (60,25)) xscaled u yscaled v;
G = ((0,20) -- (30,0) -- (60,0)) xscaled u yscaled v;

draw R cutbefore G cutafter B withcolor 2/3 red;
draw B withcolor 1/2 blue;
draw G withcolor 1/2 green;

path xx, yy;
xx = (origin -- (65u, 0)) shifted 10 down;
yy = (origin -- (0, 35v)) shifted 10 left;

drawarrow xx;
drawarrow yy;

for x = 0 step 10 until 60:
    draw (up--down) shifted (x*u, ypart point 0 of xx);
    label.bot(decimal x, (x*u, ypart point 0 of xx));
endfor

for y = 0 step 10 until 30: 
    draw (left--right) shifted (xpart point 0 of yy, y * v);
    label.lft(decimal y, (xpart point 0 of yy, y * v));
endfor

endfig;
\end{mplibcode}
\end{document}

它被包裹在中luamplib,因此您需要用它进行编译lualatex
(请注意,我使用了您在 MWE 中输入的坐标,而不是示例图中略有不同的坐标)。

答案2

带有剪辑的解决方案。

\documentclass{minimal}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}
    \begin{scope}
        \clip (0,20) -- (30,0) -- (60,0) -- (60,35) -| cycle;
        \clip (0,10) -- (30,10) -- (60,25) -- (60,35) -| cycle;
        \addplot[red] coordinates {(0,0)(15,30)(30,33)(45,28)(60,10)};
    \end{scope}
    \addplot[green] coordinates {(0,20)(30,0)(60,0)};
    \addplot[blue] coordinates {(0,10)(30,10)(60,25)};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

intersection segments借助库,您可以实现所需的结果fillbetween。如果使用一些调试代码来显示它,那么跟踪正在发生的事情会更容易一些。

% used PGFPlots v1.18.1
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
    \begin{axis}
        % for the final result change the first `red` to `none`
        \addplot [draw=red,name path=red] coordinates {(0,0)(15,30)(30,33)(45,28)(60,10)};
        \addplot [green,name path=green]  coordinates {(0,20)(30,0)(60,0)};
        \addplot [blue,name path=blue]    coordinates {(0,10)(30,10)(60,25)};

        % store the path from the first intersection of "red" and "green" to the next
        \path [
            name path=red2,
            % -------------------------
            % for debugging only
            draw=red!50!white,
            decorate,decoration={
                saw,
                post=lineto,
                post length=10pt,
            },
            % -------------------------
            intersection segments={
                of=red and green,
                sequence={L2},
            },
        ];
        % draw a path from the beginning of "red2" to the first intersection of "red2" and "blue"
        \path [
%            draw=red,
            % -------------------------
            % for debugging only
            draw=red!50!black,
            decorate,decoration={
                snake,
                post=lineto,
                post length=10pt,
            },
            % -------------------------
            intersection segments={
                of=red2 and blue,
                sequence={L1},
            },
        ];

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

该图显示了上述代码的结果

相关内容