How do I set intervals or integration with TikZ PGFlots? Something similar to the figure below, but I want to define the function, the intervals of integration and the partitions. enter image description here



\tkzDrawX \tkzDrawY
\tkzFct[line width=2pt,color = red, domain =-3:6]{(-\x-2)*(\x-5)}

  • 1
    It is always best to compose a MWE that illustrates the problem including the \documentclass so that those trying to help don't have to recreate it. Basically show us what you have tried... Oct 7, 2011 at 1:06
  • I've written a simple package, that works solely by including the pgfplots package, that draws a variety of types of Riemann sums of either fixed width rectangles of any number or of any number of varying width rectangles in conjunction with a defined function. It also has routines for combining them on one graph with appropriate order of overlap. See my answer to Placing Coloured Rectangles on a Plot Using Points From the Plot Riemann Sums Oct 31, 2013 at 1:04

2 Answers 2


If you're happy with using the midpoint of the partitions for the approximation (instead of the maximum or minimum), you can relatively easily do this in PGFplots.

I've defined a new style integral=<lower>:<upper> that draws the rectangles of the approximate integral. You can set the number of partitions by setting integral segments=<number> (default is 3).

The style could easily be adjusted to use the left or right point of the partition for the approximation.

For example

\pgfset{declare function={f=-15*(x-5)+(x-5)^3+50;}}
\addplot [thick] {f};
\addplot [
] {f}

would yield

Setting the number of partitions:

\addplot [
    integral segments=10,
] {f};
\addplot [thick] {f};

Here's the complete code:




    integral segments/.code={\pgfmathsetmacro\integralsegments{#1}},
    integral segments=3,
    integral/.style args={#1:#2}{
        ybar interval,
        x filter/.code=\pgfmathparse{\pgfmathresult-((#2-#1)/\integralsegments)/2}

\begin{tikzpicture}[/pgf/declare function={f=-15*(x-5)+(x-5)^3+50;}]
    axis lines=middle
\addplot [thick] {f};
\addplot [
] {f};


Did you take a look at the tkz-fct package? It does whole bunch of stuff, including Riemann sums.

  • Error message: "Plot data file 'riemann.tkzfct.table' not found". But i use TexLive 2011 and this is complete. Oct 7, 2011 at 1:26
  • 1
    @RegisdaSilva Could you edit your original post, and add an example showing what you did? Oct 7, 2011 at 1:37
  • Do you have gnuplot installed? The beginning of the manual (altermundus.fr/downloads/packages/tkz-fct-screen.pdf) explains how the package uses gnuplot, and how to install it. Oct 7, 2011 at 1:48
  • The gnuplot is installed but I know that I am not using. Take a simple example and please tell me the steps (commands necessary) to generate the chart. I am using pdflatex. Oct 7, 2011 at 2:12
  • Apparently in texlive 2011, gnuplot is not on the list of programs allowed to use write18. You can run pdflatex with the --shell-escape option, it will override the restrictions and execute gnuplot for you. Or you can execute gnuplot manually between runs of pdflatex. In this case, you want to run gnuplot riemann.tkzfct.gnuplot. That should create the .table file. Oct 7, 2011 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.