如何使曲线下的区域变暗?

如何使曲线下的区域变暗?

我使用 Geogebra 创建了 tikzpicture 代码。我应该对此代码进行哪些更改,以使曲线下方的区域变为阴影。tikzpicture 代码为:

\documentclass[10.pt]{book}
\usepackage[paperheight=11.69in,paperwidth=8.27in, top=1.3in, bottom=0.8in,inner=0.8in, outer=0.4in, twocolumn,twoside]{geometry}
\setlength{\columnseprule}{0.5pt}
\usepackage{amssymb,amsfonts}
\usepackage{mathrsfs}
\usepackage[centertags]{amsmath}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}
\usepackage{epsfig}
\usepackage{graphicx}\graphicspath{{Graphics/}}
\usepackage{amsthm}
\usepackage{mathptmx}
\usepackage[square,sort&compress]{natbib}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}\pagestyle{fancy}
\usepackage{xcolor}
\usepackage{setspace}
\usepackage{booktabs}
\usepackage{bigints}
\newcommand\dummy{\frac{a}{c}\,\mathrm{d}P}
%\usepackage{hyperref}
\usetikzlibrary{arrows.meta}
\renewcommand{\baselinestretch}{1.5}
\newcommand\aug{\fboxsep=-\fboxrule\!\!\!\fbox{\strut}\!\!\!}
\theoremstyle{definition}
\newtheorem{Thm}{Theorem}[section]
\newtheorem{lem}[Thm]{Lemma}
\newtheorem{pro}[Thm]{Proposition}
\newtheorem{de}[Thm]{Definition}
\newtheorem{re}[Thm]{Remark}
\newtheorem{ex}[Thm]{Example}
\newtheorem{cor}[Thm]{Corollary}
\numberwithin{equation}{section}
\definecolor{uuuuuu}{rgb}{0.26666666666666666,0.26666666666666666,0.26666666666666666}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    x=1.0cm,y=1.0cm,
    axis lines=middle,
    xmin=-2.0,
    xmax=3.2,
    ymin=-0.5,
    ymax=4.5,
    xtick={-2.0,-1.0,...,3.0},
    ytick={-0.0,1.0,...,4.0},]
    \draw [samples=50,rotate around={0.:(0.,0.)},xshift=0.cm,yshift=0.cm,line width=1.pt,domain=0:2.0)] plot (\x,{(\x)^2/2/0.5});
    \draw [line width=1.pt] (2.,0.)-- (2.,4.);
    \draw [line width=1.pt] (2.,0.)-- (0.,0.);
    \draw (0.3,3.06) node[anchor=north west] {$y=f(x)$};
    \end{axis}
    \end{tikzpicture}
\end{document}   

我需要进行一些修改,使其看起来:

在此处输入图片描述

答案1

作为谢布·格拉夫上面的评论,可能最好的方法是pgfplots使用库fillbetween。您需要在序言中调用它,然后命名限制上方(抛物线)和下方(x 轴)区域的路径。

我擅自从您的代码中删除了所有不需要的包和其他东西,并且清理了一些由 Geogebra 生成的代码。

我会这么做:

\documentclass[border=2mm]{standalone}
\usepackage               {pgfplots}
\pgfplotsset              {compat=1.17}
\usepgfplotslibrary       {fillbetween} % <-- this does the trick

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    x=1cm, y=1cm,
    axis lines=middle,
    xmin=-2,   xmax=3.2,
    ymin=-0.5, ymax=4.5,
    xtick={-2,...,3},
    ytick={1,...,4},
    xlabel=$x$, ylabel=$y$,
  ]
  \addplot [thick,red,samples=21,domain=0:2,name path=A] plot (\x,\x*\x); % parabola, we name this path 'A' for the filling
  \addplot [draw=none,name path=B] coordinates {(0,0) (2,0)};             % x-axis, we name this path 'B' for the filling
                                                                          % (we don't need to draw it again)
  \addplot [blue,dashed]           coordinates {(2,0) (2,4)};
  \addplot [red,mark=none]         coordinates {(1,3)} node {$y=f(x)$};
  \addplot[blue!20,opacity=0.5]    fill between [of=A and B];             % we fill between the paths named A and B
  \end{axis}
\end{tikzpicture}
\end{document}

编辑:或纯tikz解决方案(提供相同的输出)。

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

\begin{document}
\begin{tikzpicture}
  % axes
  \draw[-stealth] (-2,0)   -- (3.2,0) node [above left]  {$x$};
  \draw[-stealth] (0,-0.5) -- (0,4.5) node [below right] {$y$};
  % ticks
  \foreach\i in {-2,-1,1,2,3}
    \draw[very thin] (\i,0.075) --++ (0,-0.15) node [below] {$\i$};
  \foreach\i in {1,...,4}
    \draw[very thin] (0.075,\i) --++ (-0.15,0) node [left]  {$\i$};
  % area
  \fill[blue!20,opacity=0.5] (0,0) parabola (2,4) |- cycle;
  \draw[blue,dashed] (2,0) -- (2,4);
  % parabola
  \draw[thick,red] (0,0) parabola (2,4);
  \node[red] at (1,3) {$y=f(x)$};
\end{tikzpicture}
\end{document} 

在此处输入图片描述

相关内容