TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)}

share|improve this question
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... – Peter Grill Oct 7 '11 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 – Geoff Pointer Oct 31 '13 at 1:04
up vote 14 down vote accepted

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};

share|improve this answer
Thank you very much. – Regis da Silva Oct 7 '11 at 3:16

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

share|improve this answer
Error message: "Plot data file 'riemann.tkzfct.table' not found". But i use TexLive 2011 and this is complete. – Regis da Silva Oct 7 '11 at 1:26
@RegisdaSilva Could you edit your original post, and add an example showing what you did? – Torbjørn T. Oct 7 '11 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. – Jan Hlavacek Oct 7 '11 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. – Regis da Silva Oct 7 '11 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. – Jan Hlavacek Oct 7 '11 at 3:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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