如何为图表的某个区域着色?

如何为图表的某个区域着色?

我想给抛物线上方的区域上色,我在这里找到了一些建议,但我无法适应。我的代码是

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset
 {every pin/.style = {pin edge = {<-}}, 
  > = stealth, 
  flow/.style = 
   {decoration = {markings, mark=at position #1 with {\arrow{>}}},
    postaction = {decorate}
   },
  flow/.default = 0.5,   
  main/.style = {line width=1pt}
 }

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]

  \draw [main,->] (0,-0.3) -- (0,5)                   
    node [label={[above]$y$}] {};
  \draw [main,->] (-5,0) -- (5,0)           
    node [label={[right,yshift=-0.5ex]$x$}] {}; 
  \draw [main, domain=-4:4,color=red] plot (\x, {0.25*\x*\x}); 

\end{tikzpicture}
\end{document}

我不太理解代码。稍后我想对其进行修改,因为我想生成其他图形,固定在图形上方,但仅针对正 x,绘制在图形上方,但仅针对负 x,由图形和垂直轴界定,等等...

答案1

像这样 ?

截屏

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset
 {every pin/.style = {pin edge = {<-}}, 
  > = stealth, 
  flow/.style = 
   {decoration = {markings, mark=at position #1 with {\arrow{>}}},
    postaction = {decorate}
   },
  flow/.default = 0.5,   
  main/.style = {line width=1pt}
 }

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]


  \draw [main, domain=-4:4,color=red,fill=pink] plot (\x, {0.25*\x*\x}); 
  \draw [main,->] (0,-0.3) -- (0,5)                   
    node [label={[above]$y$}] {};
  \draw [main,->] (-5,0) -- (5,0)           
    node [label={[right,yshift=-0.5ex]$x$}] {}; 
\end{tikzpicture}
\end{document}

答案2

问题很笼统,因此不太清楚。一般来说,你可以向基本图表(由轴和函数曲线组成)添加任何你喜欢的内容。例如,参见

\documentclass[tikz, margin=3.14159mm]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset{
                   > = stealth,
    every pin/.style = {pin edge = {<-}},
         flow/.style = {decoration = {markings, mark=at position #1 with {\arrow{>}}},
                        postaction = {decorate}
                        },
       flow/.default = 0.5,
         main/.style = {color=#1, line width=1pt, line cap=round, line join=round},
       main/.default = black
                }

\begin{document}
    \begin{tikzpicture}
% shading function on negative domain
% it is here that it is on background of image
\fill [domain=-4:0, red!50]     plot (\x, 0.25*\x*\x) |- cycle;
% axis
\draw [main,->] (0,-0.3) -- (0,5) node [above]  {$y$};
\draw [main,->] (-5,  0) -- (5,0) node [right]  {$x$};
% function + marked 
\draw [domain=-4:4, main=red, flow=0.75]    
            plot (\x, 0.25*\x*\x);
% function + pin
\coordinate[pin=above left:{$0.35x^2$}] (aux) at (3,0.25*3*3);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容