pgfplot 填充

pgfplot 填充

我需要绘制 f、t 和 x 轴之间的图,但不知道如何绘制,也找不到解决方案。这是一个错误的解决方案

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns} % Hinzufügen der patterns-Bibliothek
\pgfplotsset{compat=1.18}


\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xlabel={$x$},
            axis x line = center, 
            ylabel={$y$},
            axis y line = center, 
            domain=.1:11,
            xmin=-1, xmax=4,
            ymin=-1, ymax=4, % Anpassung der y-Skalierung
            samples=100,
            smooth,
            no markers,
            legend pos=north east,
            grid=major, % Gitter im Hintergrund einschalten
            ]
            \addplot[name path=f,red,thick] {2/x^2};
            \addlegendentry{$f(x)=\frac{2}{x^2}$}
            \addplot[name path=t,blue,thick] {-4*x+6};
            \addlegendentry{$t(x)$}
            \addplot[name path=axis,draw=none,domain=-1:4] {0}; % x-Achse
            \addplot[gray!50, pattern=north east lines] fill between[of=f and axis, soft clip={domain=1:5}]; % Fläche unter f
            \addplot[gray!50, pattern=north west lines] fill between[of=t and axis, soft clip={domain=1:5}]; % Fläche unter t
            \path[name path=xaxis] (\pgfkeysvalueof{/pgfplots/xmin}, 0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);
        \end{axis}
    \end{tikzpicture}
\end{document}

答案1

使用intersections segments选项(参见pgfplots手动的),您可以执行以下操作:

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns} % Hinzufügen der patterns-Bibliothek
\pgfplotsset{compat=1.18}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xlabel={$x$},
            axis x line=center, 
            ylabel={$y$},
            axis y line=center, 
            domain=.1:11,
            xmin=-1, xmax=4,
            ymin=-1, ymax=4, % Anpassung der y-Skalierung
            samples=100,
            smooth,
            no markers,
            legend pos=north east,
            grid=major, % Gitter im Hintergrund einschalten
        ]
            \addplot[name path=f, red, thick] {2/x^2};
            \addlegendentry{$f(x)=\frac{2}{x^2}$}
            \addplot[name path=t, blue, thick] {-4*x+6};
            \addlegendentry{$t(x)$}
            \addplot[name path=axis, draw=none, domain=-1:4] {0}; % x-Achse

            \path[name path=f-t, intersection segments={
                    of=f and t,
                    sequence={
                        L7[reverse] -- R7
                    },
                }] -- cycle;

            \fill[pattern=north east lines, intersection segments={
                    of=f-t and axis,
                    sequence={
                        R2 -- L1
                    },
                }] -- cycle;
        \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

在第一步中,我创建了一条路径,该路径描述了由两个地块f和限定的区域,t我将 命名为。然后,我创建了一条路径,该路径描述了由和f-t限定的区域。f-taxis

请注意,这里可能由于舍入误差,路径的曲线f似乎被解释为与直线相交t多次,这就是为什么我们需要选择序列的第七部分。通常,您不会得到这样的效果,如果您更改的数量,它可能会发生变化samples

相关内容