Automatic zoom in a TGraph

Suppose I have a TGraph with a lot of points, I want to plot only a small part of them. In particual suppose the X range of the graph is from 0 to 100. I want to plot only from 0 to 50, so I can use TGraph::GetXaxis()->SetRangeUser(0,50); But what about the y range? I want to auto adjust it.

The big problem is that I want to plot the range 0->50 in a subcanvas, and the range 50->100 in another subcanvas.

TGraph* gr1 = dynamic_cast<TGraph*>(gr->Clone());
TGraph* gr2 = dynamic_cast<TGraph*>(gr->Clone());

TCanvas c; c.Divide(1,2);
c.cd(1);
gr1->Draw("AP"); gr1->GetXaxis()->SetRangeUser(0,50);
c.cd(2);
gr2->Draw("AP"); gr2->GetXaxis()->SetRangeUser(50,100);

it is not automatic, I think it’s not so difficult and very userful to have an auto scale on one axis is you manually change the scale on the other

The way to do it is via TMultiGraph

why TMultigraph? I’m plotting one TGraph for every sub canvas!

Ah ok … sorry, I read to fast. The word “auto-adjust” made me think that …
If you set a range along X the the Y range will be such that all data are shown …
I do not really understand your point.

[quote=“couet”]Ah ok … sorry, I read to fast. The word “auto-adjust” made me think that …
If you set a range along X the the Y range will be such that all data are shown …
[/quote]
yes, this is what I want

[quote=“couet”]
I do not really understand your point.[/quote]
do not?

And that’s what is done …

[quote=“couet”][quote]
If you set a range along X the the Y range will be such that all data are shown …

yes, this is what I want
[/quote]

And that’s what is done …[/quote]

Is done by who? TGraph don’t do this

Ok … better speak on a running example otherwise we will never converge… :frowning:
The following macro gives me a correct Y range.

{

   TCanvas *c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);

   const Int_t n = 20;
   Double_t x[n], y[n];
   for (Int_t i=0;i<n;i++) {
     x[i] = i*0.1;
     y[i] = 10*sin(x[i]+0.2);
   }
   gr = new TGraph(n,x,y);
   gr->Draw("ACP");
   gr->GetXaxis()->SetRangeUser(0.8,1.2);
}


[quote=“couet”]Ok … better speak on a running example otherwise we will never converge… :frowning:
The following macro gives me a correct Y range.

[code]
{

TCanvas *c1 = new TCanvas(“c1”,“A Simple Graph Example”,200,10,700,500);

const Int_t n = 20;
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++) {
x[i] = i0.1;
y[i] = 10
sin(x[i]+0.2);
}
gr = new TGraph(n,x,y);
gr->Draw(“ACP”);
gr->GetXaxis()->SetRangeUser(0.8,1.2);
}

[/code][/quote]

Why do you think this is correct? The minimum of your function in the interval is near 8.5, the maximum near 9.5. The range of the y axis is from 1 to 11. Why for example the funcion is not vertically centered, for example with an interval 8.0, 10 ?

Try to run your code without the last line, you got a plot where x range is from 0 to 2 and the y range is from… 1 to 11!!! So your last line doesn’t change the y range.

I attach an example made with gnuplot

plot [x=0.8:1.2] 10*sin(x+0.2)


TGraph is a very basic function used by many other complex objects. TGraph cannot not be too sophisticated. An automatic adjustment, as you suggest, will mess up the drawing of objects based on TGraph. We cannot do what you suggest. The automatic Y scale is done by the more complex objects like histograms.