-1

For the shown MWE:

  • I need to fill the area under the function plotted in tikz using the "Plot Command". However, using the "Fill" option in the "Draw command" does not yeild any results.

  • I need to fill the area under the plot in one occasion and in another I need to fill the area above the plot.

  • Also can I replace the fill with a pattern such as vertical lines....

  • In adition, is there a way to fill/hatch only a certain region within such plot?

\documentclass{article}
\usepackage{amsmath} 
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[help lines,step=0.5](0,0) grid(15,24);
\begin{scope}[shift={(0,6)}]
\draw[thick,color=red,fill=yellow,domain=0:6] plot (\x, {0.1*(5*-1)*(\x*6)/(12)});
\draw[thick,color=red,fill=yellow,domain=6:12] plot (\x, {0.1*(5*-1)*((12-\x)*(6))/(12)});
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}

Output for plotted function using Tikz

8
  • 1
    If you want to draw graphs, look up data visualization (TikZ) or use pgfplots (and fill between). Otherwise, provide a complete path for \fill, including all sides. Nov 20, 2020 at 14:31
  • @ John Kormylo, whey use "Data visualization" while plotting a function using a math engine is most accurate?
    – Silva
    Nov 20, 2020 at 14:35
  • Datat visualization has more to do with axes, grids and converting "furlongs per fortnight" into cm. Also, pgfmath is not accurate by any stretch of the immagination. Nov 20, 2020 at 14:39
  • However, mkaing a path or bezier curve for parabolic curves (3rd, 4th degree equations and above) is troblesome, that is why using plot equations is easier in such casese. I only used this function as a simple example however, there are occasions where I need to plot 4th degree equations, making a path or bezier curve for such equation, or even using the Hobby package is very troublesome in such case.
    – Silva
    Nov 20, 2020 at 16:00
  • 1
    What does "the area under the plot" mean? -Vertically down to the bounding box? What do you mean by "a certain region"? Nov 20, 2020 at 19:43

1 Answer 1

2

Code:

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

\begin{document}

\begin{tikzpicture}
\draw[help lines,step=0.5](0,0) grid(15,10);
\begin{scope}[shift={(0,6)}]
\fill [yellow] (0,-1.5) -- plot[domain=0:6]  (\x, {0.1*(5*-1)*(\x*6)/(12)});
\draw[thick,color=red,domain=0:6] plot (\x, {0.1*(5*-1)*(\x*6)/(12)});

\fill [yellow] (6,0)-- plot[domain=6:12]  (\x, {0.1*(5*-1)*((12-\x)*(6))/(12)});
\draw[thick,color=red,domain=6:12] plot (\x, {0.1*(5*-1)*((12-\x)*(6))/(12)});
\end{scope}
\end{tikzpicture}

\end{document}

output:

enter image description here

code2:

\begin{tikzpicture}
\draw[help lines,step=0.5](0,0) grid(15,10);
\begin{scope}[shift={(0,6)}]
\fill [yellow]  plot[domain=0:6]  (\x, {0.1*(5*-1)*(\x*6)/(12)})--(6,0);
\draw[thick,color=red,domain=0:6] plot (\x, {0.1*(5*-1)*(\x*6)/(12)});

\fill [yellow] (6,0)-- plot[domain=6:12]  (\x, {0.1*(5*-1)*((12-\x)*(6))/(12)});
\draw[thick,color=red,domain=6:12] plot (\x, {0.1*(5*-1)*((12-\x)*(6))/(12)});
\end{scope}
\end{tikzpicture}

output:

enter image description here

code3:

\begin{tikzpicture}
\draw[help lines,step=0.5](0,0) grid(15,10);
\begin{scope}[shift={(0,6)}]
\fill [yellow] (0,-1.5) -- plot[domain=0:6]  (\x, {0.1*(5*-1)*(\x*6)/(12)});
\draw[thick,color=red,domain=0:6] plot (\x, {0.1*(5*-1)*(\x*6)/(12)});

\fill [yellow]  plot[domain=6:12]  (\x, {0.1*(5*-1)*((12-\x)*(6))/(12)})--(12,-1.5);
\draw[thick,color=red,domain=6:12] plot (\x, {0.1*(5*-1)*((12-\x)*(6))/(12)});
\end{scope}
\end{tikzpicture}

output:

enter image description here

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .