Problems with setting the range of TGraph/TMultigraph

Hi ROOT-team,

I’ve got some code producing graphs and putting them into a multigraph. In principle it works well, but I have some problems with setting the range of the x-axis.

I’ve attached the C-macro saved from the canvas, one gif-file with the un-zoomed output and one with my try to set the range of the x-axis from 0 to 20. I tried changing the ranges by hand on the canvas and with SetUserRange.

As you can see, the lower bound is around -50 instead of 0 and the higher bound is around 50 as well.

What do I have to do to change the ranges to for example 0 - 20?

thanks,

Example:

{
   TGraph *g[3];
   Double_t x[10] = {0,1,2,3,4,5,6,7,8,9};
   Double_t y[10] = {1,2,3,4,5,5,4,3,2,1};
   TMultiGraph *mg = new TMultiGraph();
   for (int i=0; i<3; i++) {
      g[i] = new TGraph(10, x, y);
      g[i]->SetMarkerStyle(20);
      g[i]->SetMarkerColor(i+2);
      for (int j=0; j<10; j++) y[j] = y[j]+1;
      mg->Add(g[i]);
   }
   mg->Draw("AP");
   mg->GetXaxis()->SetTitle("E_{#gamma} (GeV)");
   mg->GetYaxis()->SetTitle("Coefficients");

   gPad->Modified();
   mg->GetXaxis()->SetRange(29,65);
}

In the example you posted it works as I would expect. But what’s the reason it doesn’t work in my real-life application?

I added the line
multigraph->GetXaxis()->SetRange(5,6);
to the C-macro i’ve attached before and still don’t get any closer to the zoom-level I want (see attachment).

It seems to me, that I have to increase the number of bins somehow… but I’m drawing graphs, not histograms, so why do I have to care about bins anyway?

thanks,

do:

   multigraph->GetXaxis()->SetLimits(0.,20.);

This solves the problem.

Thanks

  • Peter

Hi,
I was also trying to plot a graph and modify the range of the axes. I tried with ‘->SetLimits(#,#)’, ‘->SetRange(#,#)’ as well as ‘->SetRangeUser(#,#)’. Nothing worked. I also referred to the pages Page1 and Page2. I have also tried as is advised here. But with no effect. My code is attached below. Any help will be appreciated.
MyGraph.C (1.6 KB)

Regards,
Saumyen

Can you provide something we can run ? the data file is missing

Also, for next time, avoid to reuse old posts … create a new one …
This one is 10 years old !

Try:

   multigraph->Draw("AP");
   gPad->Modified(); gPad->Update();
   multigraph->GetXaxis()->SetLimits(240., 3210.);
   multigraph->SetMinimum(-1.);
   multigraph->SetMaximum(33.);
   gPad->Modified(); gPad->Update();

Sorry Olivier, actually I was looking for solution and found this post to be on the same issue. Thought it’s better that I don’t open another post. Sorry!

Thanks a lot Wile (or, whatever-the-real-name-is :yum:) . It’s working fine. But I was wondering why the command multigraph->GetXaxis()->SetLimits(240., 3210.); is not working. Can you explain a bit, please?
Anyway, thank you again.

Regards,
Saumyen

I think this command is working … see Wile example.

Yeah, extremely sorry… I actually meant to say for the Yaxis, i.e., the command : multigraph->GetXaxis()->SetLimits(240., 3210.);.

Regards,
Saumyen

The Y axis’ limits for 1D histograms are defined by the min and max of the histogram,

Okay, I see. Thank you so much Olivier…