You are here

How to set ranges on axis?

The viewing range for an histogram axis can be set from bin ifirst to ilast using the method TAxis::SetRange(ifirst, ilast). Example (hpxpy is a 2D histogram):

   TFile f("hsimple.root");

Sometimes it is more convenient to set this range using "User's Coordinates" (axis values) rather than bin numbers. To do that one should use TAxis::SetRangeUser(ufirst, ulast). This method computes the closest bin to ufirst and ulast and call TAxis::SetRange(ifirst, ilast) with this values.

The method SetAxisRange(Axis_t xmin, Axis_t xmax, Option_t *axis) allows to set the axis ranges directly on an histogram object without having to do the intermediate Get[XYZ]axis(). For instance:

   hpxpy->GetYaxis()->SetRangeUser(0., 3.);

is equivalent to:

   hpxpy->SetAxisRange(0., 3.,"Y");

SetAxisRange also performs automatically the call to SetMinimum() and SetMaximum() when the axis the the one holding the histogram content (Y axis, for 1D histograms, Z axis for 2D histograms)

For unbinned data set like graphs and multigraphs, the way to changed the axis range is to use SetLimits(vmin, vmax). This method changes the minimum and maximum values of the axis. Example:

   Int_t n=20;
   Double_t x[n],y[n];
   for (Int_t i=0; i < n; i++) {
   auto gr1 = new TGraph (n,x,y);
   auto axis = gr1->GetXaxis();

   axis->SetLimits(0.,5.);                 // along X
   gr1->GetHistogram()->SetMaximum(20.);   // along          
   gr1->GetHistogram()->SetMinimum(-20.);  //   Y