怎样填充两条曲线的界限?

怎样填充两条曲线的界限?

我想填充两条曲线的边界(如图所示)。这是我的代码

\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\definecolor{ttqqcc}{rgb}{0.2,0,0.8}
\definecolor{ffqqtt}{rgb}{1,0,0.2}
\definecolor{ttqqff}{rgb}{0.2,0,1}
\definecolor{uququq}{rgb}{0.25,0.25,0.25}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw[->,color=black] (-3.63,0) -- (3.67,0);
\foreach \x in {-3,-2,-1,1,2,3}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[color=black] (3.48,0.07) node [anchor=south west] { $x$};
\draw[->,color=black] (0,-1.93) -- (0,5.57);
\foreach \y in {-1,1,2,3,4,5}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0.06,5.19) node [anchor=west] { $y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\clip(-3.63,-1.93) rectangle (3.67,5.57);
\draw[color=ttqqff,fill=ttqqff,fill opacity=0.1, smooth,samples=50,domain=-1.7320508075688772:-1.0] plot(\x,{\x^4-4*\x^2+3}) -- (-1,0) -- (-1.73,0) -- cycle;
\draw[color=ffqqtt,fill=ffqqtt,fill opacity=0.1, smooth,samples=50,domain=-1.0:1.0] plot(\x,{\x^4-4*\x^2+3}) -- (1,0) -- (-1,0) -- cycle;
\draw[color=ttqqcc,fill=ttqqcc,fill opacity=0.1, smooth,samples=50,domain=1.0:1.7320508075688772] plot(\x,{\x^4-4*\x^2+3}) -- (1.73,0) -- (1,0) -- cycle;
\draw[smooth,samples=100,domain=-3.6342419080068153:3.665758091993186] plot(\x,{(\x)^4-4*(\x)^2+3});
\begin{scriptsize}
\fill [color=uququq] (-1.73,0) circle (1.5pt);
%\draw[color=uququq] (-1.87,-0.33) node {$A$};
\fill [color=uququq] (-1,0) circle (1.5pt);
%\draw[color=uququq] (-0.85,-0.35) node {$B$};
\fill [color=uququq] (1,0) circle (1.5pt);
%\draw[color=uququq] (1.1,0.25) node {$C$};
\fill [color=uququq] (1.73,0) circle (1.5pt);
%\draw[color=uququq] (1.84,0.25) node {$D$};
\end{scriptsize}
\end{tikzpicture}
\end{document}

在此处输入图片描述

我想要填写表格, 在此处输入图片描述 请帮帮我。

答案1

提供的代码在我的系统中产生以下输出:

在此处输入图片描述

这显示了 x 坐标为负值时的一些问题。然后我决定只对 x 坐标的正值进行操作,然后使用反射。对于所需的模式,您可以使用库 patterns

\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows,patterns}
\pagestyle{empty}

\definecolor{ttqqcc}{rgb}{0.2,0,0.8}
\definecolor{ffqqtt}{rgb}{1,0,0.2}
\definecolor{ttqqff}{rgb}{0.2,0,1}
\definecolor{uququq}{rgb}{0.25,0.25,0.25}

\begin{document}

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]

\draw[->] (-3.63,0) -- (3.67,0);
\foreach \x in {-3,-2,-1,1,2,3}
  \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) 
    node[below] {\footnotesize $\x$};
\draw[color=black] (3.48,0.07) node [anchor=south west] { $x$};

\draw[->] (0,-1.93) -- (0,5.57);
\foreach \y in {-1,1,2,3,4,5}
  \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) 
    node[left] {\footnotesize $\y$};
\draw[color=black] (0.06,5.19) node [anchor=west] { $y$};

\draw (0pt,-10pt) node[right] {\footnotesize $0$};

\clip(-3.63,-1.93) rectangle (3.67,5.57);
\draw[pattern color=ffqqtt,pattern=north east lines,fill opacity=0.1, smooth,samples=50,domain=0:1.0] 
plot(\x,{\x^4-4*\x^2+3}) -- (1,0) -- (0,0) -- cycle;
\draw[pattern color=ttqqcc,pattern=crosshatch,fill opacity=0.1, smooth,samples=50,domain=1.0:1.7320508075688772] 
plot(\x,{\x^4-4*\x^2+3}) -- (1.73,0) -- (1,0) -- cycle;

\begin{scope}[xscale=-1]
\draw[pattern color=ffqqtt,pattern=north east lines,fill opacity=0.1, smooth,samples=50,domain=0:1.0] 
  plot(\x,{\x^4-4*\x^2+3}) -- (1,0) -- (0,0) -- cycle;
\draw[pattern color=ttqqcc,pattern=crosshatch,fill opacity=0.1, smooth,samples=50,domain=1.0:1.7320508075688772] 
  plot(\x,{\x^4-4*\x^2+3}) -- (1.73,0) -- (1,0) -- cycle;
\end{scope}

\draw[smooth,samples=100,domain=-3.6342419080068153:3.665758091993186] plot(\x,{(\x)^4-4*(\x)^2+3});

\fill [color=uququq] (-1.73,0) circle (1.5pt);

\fill [color=uququq] (-1,0) circle (1.5pt);

\fill [color=uququq] (1,0) circle (1.5pt);

\fill [color=uququq] (1.73,0) circle (1.5pt);

\end{tikzpicture}
\end{document}

在此处输入图片描述

对于这种类型的图,该pgfplots包似乎是更自然的选择:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
        xmin=-3,xmax=3,
        ymin=-1.5,ymax=5,
        axis on top] 
    \addplot[color=red,domain=-2.8:2.8,samples=100] {(\x)^4-4*(\x)^2+3};

    \addplot+[mark=none,
        domain=-1:1,
        samples=100,
        pattern=north east lines,
        pattern color=red]{(\x)^4-4*(\x)^2+3} \closedcycle;

    \addplot+[mark=none,
        domain=1:1.7320508075688772,
        samples=100,
        pattern=crosshatch,
        draw=red,
        pattern color=blue!60]{(\x)^4-4*(\x)^2+3} \closedcycle;    

    \addplot+[mark=none,
        domain=-1.7320508075688772:-1,
        samples=100,
        pattern=crosshatch,
        draw=red,
        pattern color=blue!60]{(\x)^4-4*(\x)^2+3} \closedcycle;    
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

一个简短而简单的解决方案tkz-fct

\documentclass[10pt]{article}
\usepackage{tkz-fct}
\usetikzlibrary{arrows,patterns}
\pagestyle{empty}

\begin{document}

\begin{tikzpicture}[scale=1.5]
   \tkzInit[xmin=-3,xmax=3,ymin=-2,ymax=4]
   \tkzGrid
   \tkzAxeXY
   \tkzFct[domain = -2.2:2.2]{x**4-4*x**2+3}
   \tkzDrawArea[pattern=north west lines, pattern color=red!60,domain = -1:1] 
   \tkzDrawArea[pattern=north west lines, pattern color=blue!60,domain = 1.0:1.7320] 
   \tkzDrawArea[pattern=north west lines, pattern color=blue!60,domain = -1.7320:-1] 
   \tkzDefPointByFct[draw,ref=A](1)
   \tkzLabelPoint[above right](A){$x_1$}
   \tkzDefPointByFct[draw,ref=B](1.732)
   \tkzLabelPoint[above right](B){$x_2$}   
\end{tikzpicture}

\end{document} 

在此处输入图片描述

答案3

我会用pgfplots如下,注意样式的使用

截屏

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{patterns}

% set style for the axis
\pgfplotsset{mystyle/.style={
                    axis x line=middle,
                    axis y line=middle,
                    axis on top,
                    }}

% set styles for the patterns
\pgfplotsset{first pattern/.style={pattern=crosshatch,pattern color=blue}}
\pgfplotsset{second pattern/.style={pattern=north east lines, pattern color=red}}

\begin{document}
\begin{tikzpicture}[/pgf/declare function={f=x^4-4*x^2+3;}]
    \begin{axis}[mystyle,
      xmin=-4,xmax=4,
      ymin=-2,ymax=6]
        \addplot[first pattern]expression[domain=-1.7320508:-1]{f}\closedcycle;
        \addplot[second pattern]expression[domain=-1:1]{f}\closedcycle;
        \addplot[first pattern]expression[domain=1:1.7320508]{f}\closedcycle;
        \addplot[<->,samples=100] expression[domain=-3:3]{f};
    \end{axis}
\end{tikzpicture}
\end{document}

答案4

另一种pgfplots解决方案,但使用最近的(pgfplots v1.10)fillbetween库。

下面的代码展示了一种可能的解决方案。其中有一些注释,但值得一读5.6 填充pgfplots 文档

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{patterns}
% load pgfplots fillbetween library
\usepgfplotslibrary{fillbetween}

% set style for the axis
\pgfplotsset{mystyle/.style={
                    axis x line=middle,
                    axis y line=middle,
                    axis on top,blue
                    }}

% set styles for the patterns
\pgfplotsset{first pattern/.style={pattern=crosshatch,pattern color=blue}}
\pgfplotsset{second pattern/.style={pattern=north east lines, pattern color=red}}

\begin{document}
\begin{tikzpicture}[/pgf/declare function={f=x^4-4*x^2+3;}]
    \begin{axis}[mystyle,
      xmin=-4,xmax=4,
      ymin=-2,ymax=6]

%Function f is plotted and named A.
%This name will be used to plot areas between paths.
        \addplot[red, thick,<->,samples=100,name path=A] expression[domain=-3:3]{f};

%Second path (horizontal axis) is named B. 
%It's just defined, not drawn        
        \path[name path=B](axis cs:-3,0)--(axis cs:3,0);

% Area between path A and B within domain -2:2 is plotted.            
% The area is segmented (split option).
% It's possible to define special styles for every segment.
% This area consists in five segments (numbers 0 to 4).
% Only segments 1, 2 and 3 are filled. 
        \addplot[fill=none] 
            fill between[of=A and B,
                  soft clip={domain=-2:2},
                  split,
                  every segment no 1/.style={first pattern},
                  every segment no 2/.style={second pattern},
                  every segment no 3/.style={first pattern}
                  ];
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容