两个图表之间的颜色

两个图表之间的颜色

在此处输入图片描述 请告诉我如何在两个图表之间着色?f(x)= 2x^2 和 g(x)= x^4 -2x^2 这是我的代码:

\begin{center}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=0.5cm,scale=.7]
%\draw [line width=0.3pt,pink](0,0) grid (4,60);
\draw[>=latex,->,color=black,] (-4,0) -- (5,0);
\foreach \x in {-2,2}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[>=latex,->,color=black] (0,-2) -- (0,10);
\foreach \y in {}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt)node[left] {\footnotesize $\y$};
\draw[color=black] (0pt,-1pt) node[below left] {\footnotesize $0$};
\node at (5,0) [right ] {\small $x$};
\node at (0,10) [above ] {\small $y$};
%\draw [fill=red] (7,1) circle (3pt);
\draw[line width=0.5pt,color=red,smooth,samples=200,domain=-2:2] plot(\x,{(\x)^(4)-2*(\x)^(2)});
\draw[line width=0.5pt,color=red,smooth,samples=200,domain=-2:2] plot(\x,{2*(\x)^(2)});
\fill [red,fill opacity=0.3] plot [samples=5.5*\sqrtsamples,domain=0:1.41] (\x,{(\x)^(4)-2*(\x)^(2)})--(0,0)--(1.41,0);
\fill [red,fill opacity=0.3] plot [samples=5.5*\sqrtsamples,domain=-1.41:0] (\x,{(\x)^(4)-2*(\x)^(2)})--(0,0)--(-1.41,0);
\fill [red,fill opacity=0.3] plot [samples=5.5*\sqrtsamples,domain=-2:0] (\x,{(\x)^(4)-2*(\x)^(2)})--(-2,8)--(0,0);
%\draw [fill=cyan,fill opacity=0.3] plot [domain=0:2](\x,{(\x)^(4)-2*(\x)^(2)}) -- plot [domain=0:2] ((\x,{2*(\x)^(2)});
\end{tikzpicture}
\end{center}

在此处输入图片描述

答案1

这很简单pstricks

\documentclass[border=6pt, svgnames]{standalone}
\usepackage{pst-plot}

\begin{document}

\begin{pspicture}(-9,-2)(9,17)
\psset{unit=2cm, arrowinset=0.12, plotpoints=200, algebraic}
\pscustom[fillstyle=solid, fillcolor=LightPink]{%
\psplot[linewidth=0pt]{-2}{2}{x^4-2*x^2}
\psplot[linewidth=0pt]{2}{-2}{2*x^2}2}
\psaxes[showorigin=false, ticksize=-3pt 3pt, Dx=2, Dy=2]{->}(0,0)(-4.5,-1)(4.5,8.5)
\psplot[linewidth=2pt, linecolor=DarkOrange]{2}{-2}{2*x^2}
\psplot[linewidth=2pt, linecolor=RoyalBlue]{-2}{2}{x^4-2*x^2}
\end{pspicture}

\end{document}

在此处输入图片描述

答案2

因为您了解您的功能,所以您可以轻松地用它绘制它pgfplots,然后用它填充之间的区域fill between

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{samples=200}
\pgfplotsset{every tick label/.append style={font=\tiny}}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        grid=both,
        axis lines=middle,
        ymin=-1,
        ymax=8.2,
        xmin=-4.2, 
        xmax=4.2,
        grid style={line width=.1pt, draw=gray!10},
        major grid style={line width=.2pt,draw=gray!50},
        minor x tick num={4},
        minor y tick num={4},
        ]

        \addplot[name path=a,mark=none,red] {2*x^2};
        \addplot[name path=b,mark=none,blue] {x^4 -2*x^2};
        \addplot[red!20,opacity=0.5] fill between[of=a and b];

    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容