Re-applying a zoom made by the user

Dear all,

I would like to re-apply the zoom made by the user on an histogram.
This means

  1. find out the user range of the histo and
  2. set this range on the same or another histogram.

I tried the following :

[code]TCanvas c;
TH1F h(“adsf”, “asdf”, 10, 1, 10)
TAxis *axis = h.GetXaxis()

// USER : Zoom with the mouse

// find out the displayed range
xmin = gPad->GetUxmin()
xmax = gPad->GetUxmax()

// USER : Do something

// Re-apply the zoom
axis->SetRangeUser(xmin, xmax)
// --> Not what I expected as the axis moved

Unfortunately, this doesn’t work as I expected because the axis “moves”.

Then, I realized that adding or removing a little bit to xmin and xmax, does the trick :

xmin = xmin + xmin / 15; xmax = xmax - xmax / 15;

However, I am wondering if there is a nice way of doing it instead of applying this magical factor 15 ?

Thanks in advance,

That is the margins size. You should not use 15. Get the size of the margins instead.

Hi again,

It appears that the factor 15 I was applying is not working neither.

So I am interested in any way to solve this question :slight_smile:


Have you seen my reply ? it looks like we wrote the last posts at the same time. Use the margins size.


Indeed, I haven’t seen your post :slight_smile:

Thanks, I will try that.


Hi again,

Could you tell me which margin size you are refering to ?
I see several margins in TStyle, but they seem too big…


Could you reformulate your question?

if you zoom with the mouse a histogram TH1* h, then if you do again h->Draw() the histogram
witll be drawn with the range selected with the mouse, you do not have to set its range again.
If you want to set the same zoom area for another histogram h2 having the same original limits than h, you can do something like

TAxis *axis = h->GetXaxis(); int first = axis->GetFirst(); int last = axis->GetLast(); h2->GetXaxis()->SetRange(first,last);
if your second histogram h2 has different binning, do something like:

TAxis *axis = h->GetXaxis(); int first = axis->GetFirst(); int last = axis->GetLast(); h2->GetXaxis()->SetRangeUser(axis->GetBinCenter(first),axis->GetBinCenter(last));


First of all, thank you, your solution solves my problem perfectly.

Now, the question was exactly what you thought : how to apply to an histogram h2 the zoom that a user made on histogram h1.

I was stuck with gPad->GetUxmin() and didn’t think about the axis’ first and last bin.


In my case I want to zoom a TProfile. The error with using GetYAxis is the following.
“error: ‘class TProfile’ has no member named ‘GetYAxis’”

Nevertheless, it is till important for me to zoom on the Y axis.
How can I do it?

Best regards,


See also: [url]TGraph title and axis problems