# Automatic axis range for 2D and 3D histograms

**URL:** <https://root-forum.cern.ch/t/automatic-axis-range-for-2d-and-3d-histograms/19962>\
**Category:** Roofit and RooStats\
**Created:** [October 8, 2015, 10:01am UTC](https://root-forum.cern.ch/t/automatic-axis-range-for-2d-and-3d-histograms/19962 "2015-10-08T10:01:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tangzhch](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tangzhch/32/37394_2.png) [@tangzhch](https://root-forum.cern.ch/u/tangzhch)\
**Post date:** [October 8, 2015, 10:01am UTC](https://root-forum.cern.ch/t/automatic-axis-range-for-2d-and-3d-histograms/19962/1 "2015-10-08T10:01:23Z")

</div>

Hi,

I am trying to fill a 2D histogram which have unknown range of X-value, but the range of Y-axis is already known. I use the automatic axis range feature, namely set both the upper and lower limit of X-axis to 0, like this

```auto
TH2* h2 = new TH2D("h2", "2D hist", 100, 0, 0, 100, 0, 1); 
```

it works fine for the X-axis. However, it also set automatic limits for Y-axis! Having a check of the source of [TH2::BufferEmpty()](https://root.cern.ch/doc/master/TH2_8cxx_source.html#l00172), I found that it does not distinguish which axis should be automatic, and just reset both. I think it could be smarter to have separated treatment for X and Y axis, like this: (part of BufferEmpty(), not yet tested ![=P~](https://root-forum.cern.ch/uploads/default/original/2X/8/8bc7cc9f475d31e5e9c0aab57f3d28b2e765192b.gif "Drool") )

```auto
//...
/// finished checkings

#warning just a demo, not yet tested!
if (CanExtendAllAxes() || fXaxis.GetXmax() <= fXaxis.GetXmin()) {
  //find min, max of entries in buffer
  Double_t xmin = fBuffer[2];
  Double_t xmax = xmin;
  for (Int_t i=1;i<nbentries;i++) {
     Double_t x = fBuffer[3*i+2];
     if (x < xmin) xmin = x;
     if (x > xmax) xmax = x;
  }
  if (fXaxis.GetXmax() <= fXaxis.GetXmin()) {
     THLimitsFinder::GetLimitsFinder()->FindGoodLimits(this,xmin,xmax);
  } else {
     fBuffer = 0;
     Int_t keep = fBufferSize; fBufferSize = 0;
     if (xmin < fXaxis.GetXmin()) ExtendAxis(xmin,&fXaxis);
     if (xmax >= fXaxis.GetXmax()) ExtendAxis(xmax,&fXaxis);
     fBuffer = buffer;
     fBufferSize = keep;
  }
}

if (CanExtendAllAxes() || fYaxis.GetXmax() <= fYaxis.GetXmin()) {
  //find min, max of entries in buffer
  Double_t ymin = fBuffer[3];
  Double_t ymax = ymin;
  for (Int_t i=1;i<nbentries;i++) {
     Double_t y = fBuffer[3*i+3];
     if (y < ymin) ymin = y;
     if (y > ymax) ymax = y;
  }
  if (fYaxis.GetXmax() <= fYaxis.GetXmin()) {
     /// instead of 
     /// THLimitsFinder::GetLimitsFinder()->FindGoodLimits(this,xmin,xmax,ymin,ymax);
     /// fork FindGoodLimits()
     /// or we can modify FindGoodLimits()
     TAxis *yaxis = fYaxis;
     Int_t binsx = fXaxis->GetNbins(),newbinsy;
     Double_t xmin = fXaxis->GetXmin(), xmax = fXaxis->Xmax();

     if (ymin >= ymax) {
        if (yaxis->GetLabels()) {ymin = 0; ymax = ymin +yaxis->GetNbins();}
        else {ymin -= 1; ymax += 1;}
     }

     THLimitsFinder::OptimizeLimits(yaxis->GetNbins(),
           newbinsy,ymin,ymax,
           yaxis->TestBit(TAxis::kIsInteger));

     h->SetBins(binsx,xmin,xmax, newbinsy,ymin,ymax);
     /// end of insertion
  } else {
     fBuffer = 0;
     Int_t keep = fBufferSize; fBufferSize = 0;
     if (ymin < fYaxis.GetXmin()) ExtendAxis(ymin,&fYaxis);
     if (ymax >= fYaxis.GetXmax()) ExtendAxis(ymax,&fYaxis);
     fBuffer = buffer;
     fBufferSize = keep;
  }
}

/// then fill the buffer to histogram and empty the buffer
//...
```

The same problem exists in [TH3::BufferEmpty()](https://root.cern.ch/doc/master/TH3_8cxx_source.html#l00172).

---

<div class="post-metadata">

**Author:** ![dhsmith](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhsmith](https://root-forum.cern.ch/u/dhsmith)\
**Post date:** [October 8, 2015, 2:31pm UTC](https://root-forum.cern.ch/t/automatic-axis-range-for-2d-and-3d-histograms/19962/2 "2015-10-08T14:31:35Z")

</div>

Hi. We’re checking into your question. Will update here later.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [October 11, 2015, 12:42pm UTC](https://root-forum.cern.ch/t/automatic-axis-range-for-2d-and-3d-histograms/19962/3 "2015-10-11T12:42:38Z")

</div>

Hi,

Your requests make sense. since the automatic binning works for all histogram axis. I will add this as a new feature request for next release

Best Regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [December 1, 2025, 6:34pm UTC](https://root-forum.cern.ch/t/automatic-axis-range-for-2d-and-3d-histograms/19962/4 "2025-12-01T18:34:26Z")

</div>

PR: [[hist] fine-grained autorange feature for TH2/3 by ferdymercury · Pull Request #20598 · root-project/root · GitHub](https://github.com/root-project/root/pull/20598)
