Wrong range enabled by default when filling a histogram

Hi,

When running:

import ROOT

c=ROOT.TCanvas('c', '', 500, 500)
h=ROOT.TH1F('h', '', 1, 0, 0)

h.Fill('a', 1)
h.Fill('b', 1)
h.Fill('c', 1)

h.Draw()

c.SaveAs('plot.png')

I see:

image

the range does not go until the last data entry but beyond. This is even worse if you add more entries:

image

This does not make sense to me and is most likely a bug. Could you please fix it? Otherwise we have to spend our time trying to figure out how to fix it on our side, which means more code + time.

Cheers.


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.22/06
Platform: centos7
Compiler: gcc8


You need LabelsDeflate.
There is an example here.
This is not a bug. It is implemented that way for performance reasons. When you add a new labels the data structure holding the bins need to be inflated. If you do it bin per bin each time a new label shows up it is very inefficient. It is much better to increase it by a bunch of bins. Then, when a new bin shows up, the bin is already there in the data structure. It is up to the user to deflate the data structure when he knows all the labels have been added.

Hi,

Ok, thanks, I did not know that function. However no one wants to draw where there is no data. I do not believe it should be up to the user to call this function, but it should be the default behaviour that the function gets called automatically when we call TH1::Draw. Maybe that function should not even be public, but private. Otherwise you are putting a lot of work on the user when as much as possible should be automatic. I mean, we do not use computers so that we do all the work ourselves, the idea is that stuff is done for us so that we can focus on what actually matters, which is Physics.

Cheers.

It was decided to not make it automatic long time ago.
Doing it automatically at drawing time could be a way to make it more transparent for users. I personally do not see anything against that as, at drawing time, the histogram filling is supposed to be completed. I let @moneta comments on that.

Hi,

I think it is reasonable to call automatically LabelsDeflate at drawing time when plotting histogram with labels. Can you make a GitHub issue, so we will track this ?

Lorenzo

Done: Call automatically LabelsDeflate at drawing time · Issue #8598 · root-project/root · GitHub

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

And this PR does the fix. Deflate axis by couet · Pull Request #8760 · root-project/root · GitHub