How to re-apply a zoom to an histogram


I have an histogram (h1) and the user can zoom on it (click drag on the x or y axis). From time to time I get a new histogram (h2 per instance) to replace h1. I would like to have the same viewing range in h2 than in h1.

First question : how to get the viewing range ?
I see GetMaximum in the TH1 class but this is only for axis Y.
There is GetXMin in the TAxis class, but it doesn’t seem to return the viewing range.
Finally, I guess I should use GetUxmin, GetUxmax, etc… methods from the TPad, but I am not entirely sure.

Second question : How to change the viewing range of h2 ?
Using the Range method of TPad ? the TH1::SetMinimum ? or the TAxis::SetRange ?

Thanks in advance for any explanation on the differences between those methods.



On the Y axis you can do:

root [0] hpx->GetMinimum()
(const Double_t)0.00000000000000000e+00
root [1] hpx->SetMinimum(-10)
root [2] hpx->GetMinimum()   
(const Double_t)(-1.00000000000000000e+01)
root [3] 

Same thing for the Maximum.

On the X axis

root [4] TAxis *x = hpx->GetXaxis()
root [5] x->GetXmin()   
(const Double_t)(-4.00000000000000000e+00)
root [6] x->SetLimits(-20,10)
root [7] x->GetXmin()        
(const Double_t)(-2.00000000000000000e+01)
root [8] 

Hi !

Sorry for the late reply.

Your solution for the Y axis works perfectly.
However the solution for the X axis works to set new limits but what about retrieving the limits values of an histogram ?

Here is an example of what I want :

root [0] new TCanvas
(class TCanvas*)0x873dc50
root [1] h = new TH1F("test", "test", 20, 0, 20)
(class TH1F*)0x87b8ad8
root [2] h->Draw()
root [3] h->Fill(4)
root [4] // here you manually zoom on a part of X axis
root [5] TAxis *x = h->GetXaxis()
root [6] x->GetXmin()
(const Double_t)0.00000000000000000e+00

After the zoom, I want to get the new limits but I still get 0.0.

Any clue ?

Thanks for your help


Sorry for the late answer, I was away.

What about:

TAxis *ax = hpx->GetHistogram()->GetXaxis();
Double_t x1 = ax->GetBinLowEdge(ax->GetFirst());
Double_t x2 = ax->GetBinUpEdge(ax->GetLast());


Hi !

No problem. I should have replied myself with mypartial solution.
I finally do the following to get range of axes X and Y with and without zoom:

// this gives you the value taking into account the zoom
Double_t xmin = pad->GetUxmin(); 
Double_t xmax = pad->GetUxmax();
Double_t ymin = pad->GetUymin(); 
Double_t ymax = pad->GetUymax();

// and this gives you the "real" value of the axis (no zoom)
Double_t xminLimit = histo->GetXaxis()->GetXmin();  
Double_t xmaxLimit = histo->GetXaxis()->GetXmax();
Double_t yminLimit = histo->GetMinimum();
Double_t ymaxLimit = histo->GetMaximum();

I don’t know if it is the correct way of doing it, but it works. And I understand why the zoom values are stored in the pad, as you can have the same histogram displayed in two different pads.

However, I don’t understand why there is no common way of retrieving axes extremities (histo->GetXaxis()->GetXmin(); and histo->GetMinimum(); ). This is missleading.
Moreover, the synthax histo->GetXaxis()->GetXmin() leads you to search for a histo->GetYaxis()->GetYmin() which doesn’t exist. You end up with histo->GetYaxis()->GetXmin() which is non-sense and doesn’t work.

Now, what bothers me is how to set the zoom. I don’t want to set it on the histogram but on the pad (again, if I display the histogram in 2 pads, I want to put different values on each of them). I was looking for something like SetUymin(), as I used GetUymin(), but it simply doesn’t exist. How should I do it ?

thanks for your help !




Hi !

I am sorry but that doesn’t solve the problem I was mentionning: 1 histogram displayed on 2 pads (A and B). If you use histo->SetRange or whatever method on the histogram, it will be applied on both pads.
The zoom should belong to the TPad and not to the histogram. And I am sure it is the case as you can have different zoom in A and B when setting the zoom manually.

Do you know if there is a method equivalent to GetUxmin (and others) but to set these values on the pad ?



Ah yes. Sorry.

The zoom belong to the histogram. This will not be change. To draw the same histogram on two different pads and make them independent use DrawClone()


Ok, I will try that.

I know it won’t be changed ; but this is clearly inconsistent with other methods such as setDrawOptions or setLogx which belong to TPad… :slight_smile:

Thanks for your help and have a nice day !