Fill Area in TGraph

Hi Rooters

I have a TGraph and I would like to fill the area between the x-axis and the graph with some color.
The closest thing I can achieve is to use the “b” option which draws a bar.
However this methos has 2 drawbacks:

  • There is a light line on the left of every “bin”. I cannot get ride of it like with histograms using “bar0” drawing method. So my graph has some with lines in it.

  • This is an approximation since the bar is center at the graph value.

The “F” doesn’t seems to do this either

Is there a simple way of achieving this?


Use the option F and mae sur that the 1st and last point of the graph are on the X axis. Basically for a graph of n points:

x[0] = x[1]
y[0] = ymin
x[n-1] = x[n-2]
y[n-1] = ymin

Hello Olivier

We actually figured out this trick but I was hoping there was a cleaner way of doing it. It works, but it is a hack that force to create a new TGraph with points that do not belong to the graph.

An option to fill the area between [x1, x2] could be useful to show some integration area for instance.
May be in a future ROOT release this can be done?


In fact TGraph is a basic class and, even to draw an histogram (using TGraph), we do that trick internally inside root. So the F option just fill the polygon given in X and Y vectors without adding any point. Also a graph can be anything even a circle … what do you do in a such case if we implement your option ? What you are asking will work with a particular type of graph where the X array is ordered in increasing order and where for one X value you have only one Y value… so it will work for graphs looking like 1D histograms. The graphs are more general than that. You can try to use the exclusion zone technique explained here:
Or do what I told you before. Making a simple function doing the trick.

Hi Oliver

Your answer makes a lot of sense. I though a TGraph was just a reperesentation of a mathematical (cartesian) function. But since it is more general than that, I can live with the trick.