Bug in TGraph filling draw function

Hi Rene,

I am trying to use draw(‘F’) filling option to fill TGraph. But the plot doesn’t fill the curve around the x axis. Here is your example code in tutorials. I just made a little change to make the program fill the plot. The revisions are marked by red color. Could you please give me some suggestion about how to overcome this problem?

void graph() {

TCanvas *c1 = new TCanvas(“c1”,“A Simple Graph Example”,200,10,700,500);

c1->SetFillColor(42);
c1->SetGrid();

const Int_t n = [color=#FF0000]100[/color];
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++) {
x[i] = i0.1;
y[i] = TMath::Abs(10
sin(x[i]+0.2));
printf(" i %i %f %f \n",i,x[i],y[i]);
}
gr = new TGraph(n,x,y);
gr->SetLineColor(2);
gr->SetLineWidth(4);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(21);
gr->SetTitle(“a simple graph”);
gr->GetXaxis()->SetTitle(“X title”);
gr->GetYaxis()->SetTitle(“Y title”);
gr->Draw("[color=#FF0000]ACF[/color]");

// TCanvas::Update() draws the frame, after which one can change it
c1->Update();
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderSize(12);
c1->Modified();
}

Thank you so much for your help and advice! :slight_smile:

Kind regards
TGraphFill.pdf (14.3 KB)

What you get is correct. The graph filling connects the last and first points to close the polygon.

Hi Couet,

Thank you very much for your prompt reply. Is there any way to fill TGraph around x axis, instead of connecting the last and the first points? I know TH1 can do this, but I hope I can use TGraph.

Appreciate for your help!

Best

It is up to you to define the first and last points correctly ie: Yf = Yl = Ymin
TGraph is a basic graphics tool and does not do it itself.

Great! It works well by setting yf and yl to the minimum!

Thank you very much again :slight_smile: