# Automatic zoom in a TGraph

**URL:** <https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815>\
**Category:** ROOT\
**Created:** [February 4, 2011, 10:57pm UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815 "2011-02-04T22:57:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [February 4, 2011, 10:57pm UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/1 "2011-02-04T22:57:45Z")

</div>

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.

```auto
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);
```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 15, 2011, 4:13pm UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/2 "2011-02-15T16:13:52Z")

</div>

> [@How to set ranges on axis?](https://root-forum.cern.ch/t/how-to-set-ranges-on-axis/28254):
>
> The viewing range for a histogram axis can be set from bin ifirst to ilast using the method TAxis::SetRange(ifirst, ilast). Example (hpxpy is a 2D histogram): { TFile f("hsimple.root"); hpxpy-\>Draw("colz"); hpxpy-\>GetYaxis()-\>SetRange(22,23); hpxpy-\>GetXaxis()-\>SetRange(18,19); } Sometimes it is more convenient to set this range using “User’s Coordinates” (axis values) rather than bin numbers. To do that one should use TAxis::SetRangeUser(ufirst, ulast). This method computes the cl…

---

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [February 15, 2011, 4:29pm UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/3 "2011-02-15T16:29:56Z")

</div>

> [@couet](#):
>
> [http://root.cern.ch/drupal/content/how-set-ranges-axis](http://root.cern.ch/drupal/content/how-set-ranges-axis)

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 15, 2011, 4:36pm UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/4 "2011-02-15T16:36:42Z")

</div>

The way to do it is via TMultiGraph

---

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [February 15, 2011, 6:13pm UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/5 "2011-02-15T18:13:45Z")

</div>

> [@couet](#):
>
> The way to do it is via TMultiGraph

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 16, 2011, 9:09am UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/6 "2011-02-16T09:09:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [February 16, 2011, 9:32am UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/7 "2011-02-16T09:32:12Z")

</div>

[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?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 16, 2011, 9:40am UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/8 "2011-02-16T09:40:43Z")

</div>

> [@](#):
>
> 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

And that’s what is done …

---

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [February 16, 2011, 10:00am UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/9 "2011-02-16T10:00:59Z")

</div>

[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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 16, 2011, 10:12am UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/10 "2011-02-16T10:12:27Z")

</div>

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

```auto
{

   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);
}

```

![](https://root-forum.cern.ch/uploads/default/original/2X/7/74e25e9fe42c9d42c63d4ecd2aa9f35bf5b2552a.gif)

---

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [February 16, 2011, 10:52am UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/11 "2011-02-16T10:52:36Z")

</div>

[quote=“couet”]Ok … better speak on a running example otherwise we will never converge… ☹  
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] = 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);  
}

[/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

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

![](https://root-forum.cern.ch/uploads/default/original/2X/3/3a5135d8237420f1dd20041f6492b1386a87eaf7.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 16, 2011, 1:26pm UTC](https://root-forum.cern.ch/t/automatic-zoom-in-a-tgraph/11815/12 "2011-02-16T13:26:34Z")

</div>

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.
