Setting Axis Range Exactly With Variable Bins

Hi,

I have a histogram with variable size bins (for an example (0,100,150,200,500,20000)).

I want to set the X axis limits so that it will go from 0 to 1000. However, if this is done with

hist->GetXaxis()->SetRangeUser(0, 1000);

It actually sets the limits from 0 to 20,000.

How do I get ROOT to set the limits to the value I actually choose, rather than fitting in the entire last bin?

Thank you,
Jack

Hi you can set range only at the bin edges. It does not make sense having the extreme range values not at the bin edges

Lorenzo

Hi @moneta , thank you for the response but I think I might have been unclear as what I want makes sense and is what someone manually setting the axis range that has explicitly chosen to use co-ordinate values rather than bin numbers would want (and is also what the documentation for SetRangeUser claims it does “Set the viewing range for the axis from ufirst to ulast (in user coordinates).”, not set the viewing range for the axis to the lower bin edge below ufirst and the upper bin edge above ulast).

I’ve added some images below to demonstrate.

Firstly what one would get if they did hist->GetXaxis()->SetRangeUser(-0.4,0.9)
Before
However what I would like would be for ROOT to set the axis range to values I explicitly set it to, of -0.4 to 0.9. i.e. the image shown below:
AsWanted

How is this done?

Thank you for the help,
Jack

Graphs have no bins.
I think, if you draw a graph first (and it will define the axis), you should be able to set what you want.

Hi @Wile_E_Coyote, what I’m trying to draw is a TH1, the image is just an example I found to show what I would want to do. i.e. ignore the graph part of it, just the histogram part.

I’ve edited out the graph part of the image to make it more clear what I mean.

Jack

Maybe @couet can comment also on the viewing range.
For me it does not make sense for an histogram. You can always redefine a smaller binning if you need

Lorenzo

Hi,

Thank you for the suggestion but this still sets the limits to the bin edge above 1000. rather than 1000.

Jack

You could create and draw an empty histogram, with sufficiently dense x-bins. And then, on top of it, you could draw your “hist” using “same”:

{
  Double_t x[] = {0., 100., 150., 200., 500., 20000.};
  TH1F *hist = new TH1F("hist", "hist title;x axis name;y axis name",
                        ((sizeof(x) / sizeof(Double_t)) - 1), x);
  hist->SetLineColor(kRed);
  hist->Fill(0., 1.); hist->Fill(100., 2.); hist->Fill(150., 3.); hist->Fill(200., 4.); hist->Fill(500., 5.);
  //
  TH1C *h = new TH1C("h", "h", 10000, -1., 1.);
  h->SetStats(kFALSE);
  h->SetTitle(hist->GetTitle());
  h->SetXTitle(hist->GetXaxis()->GetTitle());
  h->SetYTitle(hist->GetYaxis()->GetTitle());
  h->GetXaxis()->SetLimits(hist->GetXaxis()->GetXmin(),
                           hist->GetXaxis()->GetXmax());
  h->SetMinimum(hist->GetMinimum());
  h->SetMaximum(hist->GetMaximum());
  //
  h->GetXaxis()->SetRangeUser(0., 1000.);
  h->Draw();
  hist->Draw("SAMES");
}

Hi @Wile_E_Coyote, perfect! Thank you this does exactly what I needed.

Is it possible however to add a function that does something like this to ROOT? It shouldn’t be necessary to have to do a workaround like this just to set the axis range to exactly where you want. As well as this, the documentation for setRangeUser ( https://root.cern.ch/doc/master/classTAxis.html#ac85f8261dedc23bbe68f90afd196cdb8 ) should be updated to make it clear that this sets it to the bin edge below ufirst and above ulast, unlike what the documentation currently claims.

Thanks again,
Jack