# How to set ranges on axis?

**URL:** <https://root-forum.cern.ch/t/how-to-set-ranges-on-axis/28254>\
**Category:** Howto\
**Tags:** graphics\
**Created:** [March 9, 2018, 12:11pm UTC](https://root-forum.cern.ch/t/how-to-set-ranges-on-axis/28254 "2018-03-09T12:11:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 9, 2018, 12:11pm UTC](https://root-forum.cern.ch/t/how-to-set-ranges-on-axis/28254/1 "2018-03-09T12:11:15Z")

</div>

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

```auto
{
   TFile f("hsimple.root");
   hpxpy->Draw("colz");
   hpxpy->GetYaxis()->SetRange(22,23);
   hpxpy->GetXaxis()->SetRange(18,19);
}

```

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 calls `TAxis::SetRange(ifirst, ilast)` with these values.

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

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

```

is equivalent to:

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

```

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

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

```auto
{
   Int_t n=20;
   Double_t x[n],y[n];
   for (Int_t i=0; i < n; i++) {
      x[i]=i*0.1;
      y[i]=10*sin(x[i]+0.2);
   }
   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     

   gr1->Draw("AC*");
}

```

Another method is to change the axis ranges after the graph drawing:

```auto
{
   auto c = new TCanvas("c","A Zoomed Graph",200,10,700,500);

   int n = 10;
   double x[10] = {-.22,.05,.25,.35,.5,.61,.7,.85,.89,.95};
   double y[10] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};

   auto gr = new TGraph(n,x,y);
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(20);
   gr->Draw("ALP");
   gr->GetXaxis()->SetRangeUser(0, 0.5);
   gr->GetYaxis()->SetRangeUser(1, 8);
}

```

It is also possible to draw a frame with the wanted limits and then draw the graph. This method allows specifying larger axis ranges than the original one.

```auto
{
   auto c = new TCanvas("c","A Zoomed Graph",200,10,700,500);
   c->DrawFrame(0,1,0.5,8);

   int n = 10;
   double x[10] = {-.22,.05,.25,.35,.5,.61,.7,.85,.89,.95};
   double y[10] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};

   auto gr = new TGraph(n,x,y);
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(20);
   gr->Draw("LP");
}

```
