# Getting Y axis limits on a TF1

(Sorry for reposting, looks like I put this in the wrong forum originally.)

Seems like this should be trivially easy, but I can’t figure it out. Suppose I plot a function and ask about its y axis:

void foo()
{
TF1* ffoo = new TF1 (“ffoo”, “pol2”, 0, 1000.);
ffoo->SetParameters (30000., .05, .03);
ffoo->Draw();
TAxis* ffy = ffoo->GetYaxis();
cout << ffy->GetXmin() << " " << ffy->GetXmax() << endl;
}

I’d expect in this case to get something a little under 30000 and something a little over 60000. Instead I get 0 and 1. What am I missing?

``````void foo()
{
TF1* ffoo = new TF1 ("ffoo", "pol2", 0, 1000.);
ffoo->SetParameters (30000., .05, .03);
ffoo->Draw();
cout << ffoo->GetHistogram()->GetMinimum() << " "
<< ffoo->GetHistogram()->GetMaximum() << endl;
}``````

[quote=“couet”]``` cout << ffoo->GetHistogram()->GetMinimum() << " " << ffoo->GetHistogram()->GetMaximum() << endl; ```[/quote]

Unless I’m much mistaken that’s not the same thing. That returns the maximum and minumum values of the histogram, does it not? What I was asking about was the extreme values of the y axis, which in general are not equal to the histogram extrema.

Yes, you are right the maximum on the graphical Y axis has a margin on top to make the plot look nicer. What you want is:

``````void foo()
{
TF1* ffoo = new TF1 ("ffoo", "pol2", 0, 1000.);
ffoo->SetParameters (30000., .05, .03);
ffoo->Draw();
cout << ffoo->GetHistogram()->GetMinimum() << " "
<< ffoo->GetHistogram()->GetMaximum()*(1+gStyle->GetHistTopMargin())
<< endl;
}``````

That’ll probably do for what I had in mind. But what if the user has zoomed in? Then that still doesn’t give the axis limits.

I just tried to execute your macro, zoom Y Axis, and do the cout again. I get the new values.

On closer examination I see I’d misunderstood Couet’s macro. Yet it seems to make the problem worse rather than better – there seem to be inconsistencies in ROOT.

If I run the macro I get 28513.5 62738 which indeed seems to correspond to the limits of the Y axis. But that’s the first inconsistency: Why does ffoo->GetHistogram()->GetMinimum() return 28513.5 – the minimum of the function minus a margin – while ffoo->GetHistogram()->GetMaximum() return 59750.5 – the maximum of the function without a margin?

In other words, on an unzoomed function plot, GetMinimum() gives the correct lower limit of the y axis but GetMaximum() does not give the correct upper limit, and needs to have the margin added.

But if I use the mouse to zoom to about y = 35000 to 50000 and do ffoo->GetHistogram()->GetMinimum() and ffoo->GetHistogram()->GetMaximum() I get 35011 and 55076 – this time both GetMinimum() and GetMaximum() return the correct lower and upper limits, and if you add the margin to GetMaximum() you get the wrong value. This is a second inconsistency.

``````root  gPad->GetUymin()