O clipe suave não funciona em áreas sombreadas entre curvas

O clipe suave não funciona em áreas sombreadas entre curvas

Tenho tentado encontrar uma maneira de sombrear a área entre as curvas, mas quando tento codificar em LaTeX com softclip, tudo desliga. Eu não sei o que fazer.

Este é o meu código:

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    grid=both,
    axis lines=middle,
    xmin=-2, xmax=5,
    ymin=-5, ymax=10,
    xlabel={$x$},
    ylabel={$y$},
    legend style={at={(0.5,-0.15)}, anchor=north},
    ]

    \addplot[blue, domain=-2:5, samples=100, name path=A] {x^2 - 2*x};
    \addlegendentry{$y = x^2 - 2x$}

    \addplot[red, domain=-2:5, samples=100, name path=B] {4*x - x^2};
    \addlegendentry{$y = 4x - x^2$}

    % Create a path for the shaded region
    \path[name path=zero] (0,0) -- (0,8);

    % Shade the region between the two functions in red
    \addplot[red, fill, opacity=0.3] fill between[of=A and B, soft clip={domain=0:3}] \closedcycle;

  \end{axis}
\end{tikzpicture}

\end{document}

Responder1

Quando tento executar seu código, vejo esta mensagem: ! Package pgfplots Error: 'name path' is undefined. Please load \usetikzlibrary {intersections} or \usepgfplotslibrary{fillbetween}.

Então eu fiz isso. Eu inseri \usepgfplotslibrary{fillbetween}no preâmbulo. Agora parece funcionar perfeitamente para mim.

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    grid=both,
    axis lines=middle,
    xmin=-2, xmax=5,
    ymin=-5, ymax=10,
    xlabel={$x$},
    ylabel={$y$},
    legend style={at={(0.5,-0.15)}, anchor=north},
    ]

    \addplot[blue, domain=-2:5, samples=100, name path=A] {x^2 - 2*x};
    \addlegendentry{$y = x^2 - 2x$}

    \addplot[red, domain=-2:5, samples=100, name path=B] {4*x - x^2};
    \addlegendentry{$y = 4x - x^2$}

    % Create a path for the shaded region
    \path[name path=zero] (0,0) -- (0,8);

    % Shade the region between the two functions in red
    \addplot[red, fill, opacity=0.3] fill between[of=A and B, soft clip={domain=0:3}] \closedcycle;

  \end{axis}
\end{tikzpicture}

\end{document}

contanto que seja isso que você queria...

insira a descrição da imagem aqui

informação relacionada