Histogram x-axis range


Is there way to zoom the x-axis viewing range in a histogram? I tried SetRangeUser() but did not work.


h->GetXaxis()->SetRangeUser(min, max);

Thanks. I tried the same thing but did not work. It works for y-axis. Also, it work for TGraph.

see How to set ranges on axis?

1 Like

Thanks, I appreciate. It is for TGraph. Is there way to set for histogram without converting it to TGraph.

On the Y axis for histograms you should use SetMaximum() and SetMinimum() (quite clear from the example I sent)

Ok. It seems like h->GetXaxis()->SetLimits(min, max) works but different way. It does not zoom out the plot, it only zoom in the x-axis. I have attached two plots, before and after h->GetXaxis()->SetLimits(min, max).

Thanks.c2.pdf (14.1 KB)c1.pdf (13.9 KB)

To zoom you should do:


SetLImits changes the limits of the histogram but does not zoom. The histograms remains the same only the limits on the axis change.

Hi couet,

Right. Unfortunately, h->GetXaxis()->SetRangeUser(min,max) works if min and max are within the original min and max range. If my original min = -1 and max = 1, then it works if I do SetRangeUser(-0.7, 0.7) but it does not work if I do SetRangeUser(-1.4, 1.4).

I don’t think you can do this…

Thanks. Any trick?

You need to re-book and re-fill the histogram. Eventually if you do not know the initial range you can use the buffer (fill with xmin> xmax).