如何在交叉点和曲线下填充颜色?

如何在交叉点和曲线下填充颜色?

我的MWE如下:

\usepackage{tikz,pgfplots,tikz-3dplot}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc,quotes,angles,intersections,decorations.pathreplacing,decorations.markings,math,shapes.geometric,shapes.misc,positioning,hobby,bending,shadings,fit,arrows,shapes.arrows,fadings,patterns,patterns.meta}
\usepgfplotslibrary{polar,colorbrewer,fillbetween,groupplots}


\begin{document}
\begin{tikzpicture}[
  declare function={
    f(\x)=pow(\x,2)+2;
    g(\x)=pow(\x,4)-2*pow(\x,2)+1;
  }]
\begin{axis}[
  axis lines*=middle, axis line style={-stealth'}, major tick length=2pt,
  every axis x label/.style={at={(ticklabel* cs:1)},anchor=west,}, xlabel=$x$,
  every axis y label/.style={at={(ticklabel* cs:1)},anchor=south,}, ylabel={$y$},
  enlargelimits=upper, % ticks=none,
  ymin=-0.5, xmin=-2.3,
  ]

  \addplot[red,smooth,domain=-2:2,line width=1,name path=curve_f] {f(\x)};
  \addplot[blue,smooth,domain=-1.85:1.85,line width=1,name path=curve_g] {g(\x)};

  \path[domain=-1.85:0,name path=f(x)_1] plot (\x,{f(\x)});
  \path[domain=-2:0,name path=g(x)_1] plot (\x,{g(\x)});
  \path[name intersections={of=f(x)_1 and g(x)_1,by=A}];
  \node[circle,fill=green,minimum size=6pt,inner sep=0pt] at (A){};

  \draw[gray,dashed] (A) -- ($(0,0)!(A)!(1,0)$);
\end{axis}
\end{tikzpicture}
\end{document}

我想要填充用绿色标记的交叉点 A 下方和用蓝色标记的曲线上的颜色,如下图所示:

在此处输入图片描述

答案1

您应该检查是否只加载了真正需要的库和包:pgfplots已经加载的tikz、您不使用的tikz-3dplot以及您加载的大多数库。

以下内容大致取自PGFlots 手册

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}[
  declare function={
    f(\x)=pow(\x,2)+2;
    g(\x)=pow(\x,4)-2*pow(\x,2)+1;
  }]
\begin{axis}[
  axis lines*=middle, axis line style={-stealth'}, major tick length=2pt,
  every axis x label/.style={at={(ticklabel* cs:1)}, anchor=west}, 
  xlabel=$x$,
  every axis y label/.style={at={(ticklabel* cs:1)}, anchor=south}, 
  ylabel={$y$},
  enlargelimits=upper, % ticks=none,
  ymin=-0.5, xmin=-2.3,
  ]

  \addplot[red, smooth, domain=-2:2, line width=1, name path=curve_f] {f(\x)};
  \addplot[blue, smooth, domain=-1.85:1.85, line width=1, name path=curve_g] {g(\x)};

  \path[domain=-1.85:0, name path=f(x)_1] plot (\x, {f(\x)});
  \path[domain=-2:0, name path=g(x)_1] plot (\x, {g(\x)});
  \path[name intersections={of=f(x)_1 and g(x)_1, by=A}];
  \node[circle, fill=green, minimum size=6pt, inner sep=0pt] at (A) {};

  \draw[gray, dashed] (A) -- (A |- 0,0);

  \path[name path=base]
    (\pgfkeysvalueof{/pgfplots/xmin},0) --
    (\pgfkeysvalueof{/pgfplots/xmax},0);

  \addplot[red] fill between [of=g(x)_1 and base, soft clip={(A) rectangle (-1,0)}];
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者如果您也想使用它intersections来识别蓝色曲线的左最小值:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}[
  declare function={
    f(\x)=pow(\x,2)+2;
    g(\x)=pow(\x,4)-2*pow(\x,2)+1;
  }]
\begin{axis}[
  axis lines*=middle, axis line style={-stealth'}, major tick length=2pt,
  every axis x label/.style={at={(ticklabel* cs:1)}, anchor=west}, 
  xlabel=$x$,
  every axis y label/.style={at={(ticklabel* cs:1)}, anchor=south}, 
  ylabel={$y$},
  enlargelimits=upper, % ticks=none,
  ymin=-0.5, xmin=-2.3,
  ]

  \addplot[red, smooth, domain=-2:2, line width=1, name path=curve_f] {f(\x)};
  \addplot[blue, smooth, domain=-1.85:1.85, line width=1, name path=curve_g] {g(\x)};

  \path[domain=-1.85:0, name path=f(x)_1] plot (\x, {f(\x)});
  \path[domain=-2:0, name path=g(x)_1] plot (\x, {g(\x)});

  \path[name intersections={of=f(x)_1 and g(x)_1, by=A}];
  \node[circle, fill=green, minimum size=6pt, inner sep=0pt] at (A) {};

  \draw[gray, dashed] (A) -- (A |- 0,0);

  \path[name path=base]
    (\pgfkeysvalueof{/pgfplots/xmin},0) --
    (\pgfkeysvalueof{/pgfplots/xmax},0);

  \path[name intersections={of=base and g(x)_1, by=B}];
  \node[circle, fill=green, minimum size=6pt, inner sep=0pt] at (B) {};

  \addplot[red] fill between [of=g(x)_1 and base, soft clip={(A) rectangle (B)}];
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容