3

I currently have this code:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}

\definecolor{linecolor1}{HTML}{3DC6F3}
\pgfplotsset{integration/.style={color=linecolor1,mark=none,line width=0.5pt,solid}}

\pgfplotsset{every axis/.append style={
    axis x line=middle,
    axis y line=middle,
    ticks=none,
    axis line style={->},
    xlabel={$x$},
    ylabel={$y$},
    samples=1000,
  },
}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
        xmin=-2,xmax=4,
        ymin=-2,ymax=8,
      ]
      \addplot[integration]{-(1/4)*x*(x+2)*(x-1)*(x-4)};
    \end{axis}
  \end{tikzpicture}
\end{document}

which creates the following enter image description here

How can I create the integration style to shade the area under the curve?

3
  • 1
    Alright. Made my question clearer. Commented Nov 24, 2022 at 5:49
  • 2
    Welcome to TeX.SX! You seem to be asking two [or more] unrelated questions, here. On TeX.SX, we try to keep unrelated questions on separate pages. If you have multiple questions that are unrelated to one another, you should ask each in a separate TeX.SX "question". You'll stand a better chance of getting a satisfactory answer to each of your questions.
    – dexteritas
    Commented Nov 24, 2022 at 9:55
  • Important: When using PGFPlots always set version(compat) : \pgfplotsset{compat=1.18} Commented Nov 25, 2022 at 1:33

1 Answer 1

3

If you want to fill everything below the curve add fill=blue!30 to integration/.style. If it should be filled between the curve and the x-axis, see below.

Adaptations

Variant 1

  • add second plot at 0 (\addplot [name path=xaxis] {0}) OR
    • if it is excactly the x-axis just set x axis line style={name path=xaxis} (see comment by hpekristiansen)
  • give the function a name with name path (e.g., curve)
  • add \usepgfplotslibrary{fillbetween} that defines the fill between command:
    \addplot[fill=blue!30] fill between[of=curve and xaxis];
    

Variant 2

  • use fill=blue!30 directly on the plot and add \closedcycle (this will draw a line over the x-axis) (see comment by hpekristiansen)

Result

Variant 1:

enter image description here

Variant 2:

enter image description here

Code

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{fillbetween}

\definecolor{linecolor1}{HTML}{3DC6F3}
\pgfplotsset{
    integration/.style={color=linecolor1,mark=none,line width=0.5pt,solid},
    every axis/.append style={
        axis x line=middle,
        axis y line=middle,
        ticks=none,
        axis line style={->},
        xlabel={$x$},
        ylabel={$y$},
        samples=1000,
        x axis line style={name path=xaxis},
    },
}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=-2,xmax=4,
            ymin=-2,ymax=8,
            domain=-5:5,
        ]
            % variant 1:
            \addplot [name path=curve, integration]{-(1/4)*x*(x+2)*(x-1)*(x-4)};
            \addplot [fill=blue!30] fill between[of=curve and xaxis];
            %
            % variant 2:
            %\addplot [name path=curve, integration, fill=blue!30]{-(1/4)*x*(x+2)*(x-1)*(x-4)} \closedcycle;
        \end{axis}
    \end{tikzpicture}
\end{document}
4
  • 1
    +1. One would normally not want to add extra plots. This messes with the legend and gives problem with the domain used with larger limits. -add lots of extra points, line segments -worse compile time and output. Use e.g. x axis line style={name path=xaxis}. Commented Nov 24, 2022 at 18:47
  • Is there a way to have the integration style fill in the curve for me so I don't have to add an extra line? Also, like @hpekristiansen suggested, I'm using x axis line style={name path=xaxis} and y axis line style={name path=yaxis} so I don't have to create another plot. Also, quick note, is it alright if I provide the name of the curve in the integration style? Commented Nov 25, 2022 at 1:20
  • 1
    Yes: - add name path=function to the style. I tried and failed to add the fill plot to the style with execute at end plot - but that is not allowed. An other alternative is to have the fill in the plot itself: \addplot[integration, fill=cyan!30]{-(1/4)*x*(x+2)*(x-1)*(x-4)} \closedcycle; (Does not use fillbetween - the problem here is that the function(path) is also drawn along the x axis). Commented Nov 25, 2022 at 1:41
  • @hpekristiansen thanks for the suggestions. I added it as a variant to the answer.
    – dexteritas
    Commented Nov 25, 2022 at 11:25

You must log in to answer this question.

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