Draw vertical error bars of TGraphErrors

Consider this example:

int datagr()
{
  TH1F * h = new TH1F("h", "h", 4, -2, 2);
  h -> Sumw2();
  h -> FillRandom("gaus", 500);
  //h -> Print("all");                                                          
  TGraphErrors * gr = new TGraphErrors(h);
  TCanvas * c = new TCanvas;
  gr -> Draw("AP");
  gr -> SetMarkerStyle(20);
  gr -> SetMarkerSize(1.0);
  gr -> SetLineWidth(0);
  return 0;
}

The output is:

How can I have the vertical graph error bars drawn?


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


If you change the line width to zero, how do you expect to see the lines from the errors? Remove that line and you will see error bars being drawn.

Then also the horizontal bars are shown. The question is about displaying the vertical error bars.

TGraphErrors has vertical and horizontal error bars. They are defined when you create the TGraphErrors. If you do not have errors along the X axis simply define these errors to 0.

How to define the errors along X to 0?

void datagr()
{
   TH1F * h = new TH1F("h", "h", 4, -2, 2);
   h -> Sumw2();
   h -> FillRandom("gaus", 500);
   TGraphErrors * gr = new TGraphErrors(h);
   gr -> SetMarkerStyle(20);
   gr -> SetMarkerSize(1.0);
   int n = gr->GetN();
   double ey;
   for (int i=0; i<n; i++) {
      ey = gr->GetErrorY(i);
      gr->SetPointError ( i, 0, ey);
   }
   gr -> Draw("AP");
}
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.