# Problem in setting the range of the plot

**URL:** <https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089>\
**Category:** ROOT\
**Created:** [May 24, 2022, 8:41am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089 "2022-05-24T08:41:50Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![AleCalamida](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@AleCalamida](https://root-forum.cern.ch/u/AleCalamida)\
**Post date:** [May 24, 2022, 8:41am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/1 "2022-05-24T08:41:50Z")

</div>

Dear Root experts,

I have a problem on setting the range of a TGraphErrors plot. The comands that I wrote in the macro are the following:

```
  g->GetXaxis()->SetTitle("E (MeV)");   
  g->GetXaxis()->SetRangeUser(1e-9,10.);   
  g->GetYaxis()->SetTitle("Estimated/Ref");
  g->GetYaxis()->SetRangeUser(0.,4.5);
  gPad->Modified();
  gPad->Update(); 

```

For the y-axis no problem. However, for the x-axis it do not change whatever values I put in the comand GetXaxis. I am writing something wrong in the code. The plot is on log scale on the x-axis.

Best regards and thank you for your time, Alessandro Calamida.

ROOT Version: 6.2  
Platform: Ubuntu  
Compiler: gcc9.4.0

---

<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:** [May 24, 2022, 9:22am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/2 "2022-05-24T09:22:20Z")

</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:** ![AleCalamida](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@AleCalamida](https://root-forum.cern.ch/u/AleCalamida)\
**Post date:** [May 24, 2022, 9:31am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/3 "2022-05-24T09:31:34Z")

</div>

I do not have an histogram. This is a TGraphError plot. I set the range of the axis as the manual says. Unfortunately for the x-axis they do not change, whatever value I put in it.

---

<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:** [May 24, 2022, 9:33am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/4 "2022-05-24T09:33:16Z")

</div>

This how-to also explain it for graphs.  
Can you provide a macro (complete) reproducing the problem ?

---

<div class="post-metadata">

**Author:** ![AleCalamida](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@AleCalamida](https://root-forum.cern.ch/u/AleCalamida)\
**Post date:** [May 24, 2022, 10:08am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/5 "2022-05-24T10:08:20Z")

</div>

```
 ifstream input("");
                 
  vector<double> x;
  vector<double> y;
  vector<double> ex;
  vector<double> ey;    
  
  Double_t outx=0;
  //Double_t outx2=0;
  //Double_t outxmed=0;
  Double_t outx_err=0;
  Double_t outy=0;
  Double_t outy_err=0;  
        
  if(!input){cout<<"errore apertura file"<<endl;}            
  
  while(input>>outx>>outy>>outy_err){
   //outxmed=(outx1+outx2)/2;
   x.push_back(outx);
   y.push_back(outy);
   ex.push_back(0);
   ey.push_back(outy_err);
  }
  Int_t j=x.size();     
            
  TCanvas *spectrum=new TCanvas("Spectrum","Spectrum");   
  spectrum->SetLogx();        
  
  TGraphErrors *g=new TGraphErrors(j,&x[0],&y[0],&ex[0],&ey[0]);
  g->SetTitle("");
  g->SetLineWidth(2);
  g->SetLineColor(4);
  g->SetMarkerStyle(21);
  g->SetMarkerColor(4);
  
  g->Draw("AP");
  
  /*TLegend *legend = new TLegend(0.15,0.55,0.52,0.7);

  legend->SetFillColor(0); 
  legend->SetLineColor(1);
  legend->SetTextSize(0.04);
  legend->AddEntry(g,"","lep");
  legend->Draw();*/     
  
  g->GetXaxis()->SetTitle("E (MeV)");   
  g->GetXaxis()->SetRangeUser(1e-9,10.);   
  g->GetYaxis()->SetTitle("Estimated/Ref");
  g->GetYaxis()->SetRangeUser(0.,4.5);
  gPad->Modified();
  gPad->Update();                       
  
  spectrum->SaveAs(""); 
  
   x.clear();
   y.clear();
   ex.clear();
   ey.clear();
```

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [May 24, 2022, 10:44am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/6 "2022-05-24T10:44:50Z")

</div>

I can confirm that changing axis range for `TGraph` never worked for me as well, actually:

```auto
root --version
ROOT Version: 6.26/00
Built for linuxx8664gcc on Mar 03 2022, 06:51:13
From tags/v6-26-00@v6-26-00

```

Reproducable:

```cpp
$ root
[0] TGraph gr
root [1] gr.SetPoint(0, 1, 1)
root [2] gr.SetPoint(1, 2, 4)
root [3] gr.SetPoint(2, 3, 9)
root [4] gr.SetPoint(3, 4, 16)
root [5] gr.SetPoint(4, 5, 25)
root [6] gr.Draw()
// Trying to change canvas range through the canvas interface
// 1. right-click on the x axis
// SetRangeUser() -> (-20, 20) -- doesn't work

```

---

<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:** [May 24, 2022, 10:56am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/7 "2022-05-24T10:56:31Z")

</div>

```auto
void foxwise() {
   auto C = new TCanvas();
   C->Divide(2,1);

   // SetRange can define smaller ranges (zoom)
   C->cd(1);
   auto gr1 = new TGraph();
   gr1->AddPoint(1, 1);
   gr1->AddPoint(2, 4);
   gr1->AddPoint(3, 9);
   gr1->AddPoint(4, 16);
   gr1->AddPoint(5, 25);
   gr1->Draw();
   gr1->GetXaxis()->SetRangeUser(3, 4);

   // Wider Ranges can be done this way:
   C->cd(2)->DrawFrame(-20, 0., 20., 30);
   auto gr2 = new TGraph();
   gr2->AddPoint(1, 1);
   gr2->AddPoint(2, 4);
   gr2->AddPoint(3, 9);
   gr2->AddPoint(4, 16);
   gr2->AddPoint(5, 25);
   gr2->Draw();
}

```

 ![Screenshot 2022-05-24 at 12.55.59](https://root-forum.cern.ch/uploads/default/original/3X/6/b/6b4ee706c06cad85c3b640e9a566c6d9976601a8.png)

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [May 24, 2022, 11:14am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/8 "2022-05-24T11:14:17Z")

</div>

Thanks for the clarification.

I see the second method in the linked HowTo. However, it is just mentioned as an alternative, while I think it is fair to specify it is as the only working option for “unzooming”.

Also, as far as I understand, it makes drawing wider range from the canvas interface after the graph is drawn impossible?

I don’t know the implementation details and why it is impossible to unzoom with TGraph’s `TAxis`, while it works with TH1’s `TAxis` but I think it would be nice to have consistent behaviour with both. As it is very confusing for newbies and not only.  
Would it be possible to overload the SetRange() behaviour to call back to DrawFrame() in case of the TGraph?

---

<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:** [May 24, 2022, 11:20am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/9 "2022-05-24T11:20:44Z")

</div>

It is the same with histograms:

```auto
root [0] auto h = new TH1F("h","h",100,-4,4)
root [1] h->Draw();
root [2] h->GetXaxis()->SetRangeUser(-10,10)

```

> [@FoxWise](#):
>
> think it is fair to specify it is as the only working option for “unzooming”.

yes good point

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 24, 2022, 11:27am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/10 "2022-05-24T11:27:04Z")

</div>

Actually, for “graphs”, it is very easy to use any “arbitrary values” for the x-axis range:  
`graph->GetHistogram()->GetXaxis()->SetLimits(xmin, xmax);`

---

<div class="post-metadata">

**Author:** ![AleCalamida](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@AleCalamida](https://root-forum.cern.ch/u/AleCalamida)\
**Post date:** [May 24, 2022, 11:28am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/11 "2022-05-24T11:28:24Z")

</div>

> [@couet](#):
>
> `C->cd(2)->DrawFrame(-20, 0., 20., 30);`

I tried in this way as suggested but unfortunately it did no work. I wrote the whole code down there:

R\_\_LOAD\_LIBRARY(libTreePlayer)  
#include   
#include   
#include   
#include   
#include

void Graph\_Single(){

```
 ifstream input("");
                 
  vector<double> x;
  vector<double> y;
  vector<double> ex;
  vector<double> ey;    
  
  Double_t outx=0;
  //Double_t outx2=0;
  //Double_t outxmed=0;
  Double_t outx_err=0;
  Double_t outy=0;
  Double_t outy_err=0;  
        
  if(!input){cout<<"errore apertura file"<<endl;}            
  
  while(input>>outx>>outy>>outy_err){
   //outxmed=(outx1+outx2)/2;
   x.push_back(outx);
   y.push_back(outy);
   ex.push_back(0);
   ey.push_back(outy_err);
  }
  Int_t j=x.size();     
            
  TCanvas *spectrum=new TCanvas("Spectrum","Spectrum");   
  spectrum->SetLogx(); 
  spectrum->cd(1)->DrawFrame(1e-10, 0., 10., 5.);       
  
  TGraphErrors *g=new TGraphErrors(j,&x[0],&y[0],&ex[0],&ey[0]);
  g->SetTitle("");
  g->SetLineWidth(2);
  g->SetLineColor(4);
  g->SetMarkerStyle(21);
  g->SetMarkerColor(4);
  
  g->Draw("AP");
  
  /*TLegend *legend = new TLegend(0.15,0.55,0.52,0.7);

  legend->SetFillColor(0); 
  legend->SetLineColor(1);
  legend->SetTextSize(0.04);
  legend->AddEntry(g,"","lep");
  legend->Draw();*/     
  
  g->GetXaxis()->SetTitle("E (MeV)");   
  //g->GetXaxis()->SetRangeUser(1e-9,10.);   
  g->GetYaxis()->SetTitle("Estimated/Ref");
  //g->GetYaxis()->SetRangeUser(0.,4.5);
  gPad->Modified();
  gPad->Update();                       
  
  spectrum->SaveAs(""); 
  
   x.clear();
   y.clear();
   ex.clear();
   ey.clear();

```

}

---

<div class="post-metadata">

**Author:** ![AleCalamida](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@AleCalamida](https://root-forum.cern.ch/u/AleCalamida)\
**Post date:** [May 24, 2022, 11:29am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/12 "2022-05-24T11:29:02Z")

</div>

It is not an histogram, I tried that option but it do not work

---

<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:** [May 24, 2022, 11:36am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/13 "2022-05-24T11:36:26Z")

</div>

Your data file is missing but this version is ok:

```auto
void Graph_Single(){

 ifstream input("");

  vector<double> x;
  vector<double> y;
  vector<double> ex;
  vector<double> ey;

  Double_t outx=0;

  Double_t outx_err=0;
  Double_t outy=0;
  Double_t outy_err=0;

  if(!input){cout<<"errore apertura file"<<endl;}

  while(input>>outx>>outy>>outy_err){
   x.push_back(outx);
   y.push_back(outy);
   ex.push_back(0);
   ey.push_back(outy_err);
  }
  Int_t j=x.size();

  TCanvas *spectrum=new TCanvas("Spectrum","Spectrum");
  spectrum->SetLogx();
  spectrum->DrawFrame(1e-10, 0., 10., 5.);

  TGraphErrors *g=new TGraphErrors(j,&x[0],&y[0],&ex[0],&ey[0]);
  g->SetTitle("");
  g->SetLineWidth(2);
  g->SetLineColor(4);
  g->SetMarkerStyle(21);
  g->SetMarkerColor(4);

  g->Draw("P");
![Screenshot 2022-05-24 at 13.36.03|690x456](upload://oTrSQNyLu1y7g8w7qdjMYPmD7W6.png)

}

```

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [May 24, 2022, 11:45am UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/14 "2022-05-24T11:45:55Z")

</div>

ah, you are right. I got confused, because for histograms the drawing range is set to the initially specified bin range (-4, 4), independently if there is any data or not in the drawing range.  
So I didn’t even noticed this limitation for histograms.

While for the TGraph range is always re-adjusts between x\_min/x\_max of the data points.  
Which becomes very noticable if one tries to plot 100 scan plots within same x/y range, which doesn’t work out of the box as for the histograms, but requires preliminary `c->DrawFrame()`

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 24, 2022, 12:54pm UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/15 "2022-05-24T12:54:35Z")

</div>

@FoxWise

> [@Problem in setting the range of the plot](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/10):
>
> Actually, for “graphs”, it is very easy to use any “arbitrary values” for the x-axis range: graph-\>GetHistogram()-\>GetXaxis()-\>SetLimits(xmin, xmax);

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [June 7, 2022, 12:55pm UTC](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/16 "2022-06-07T12:55:09Z")

</div>

Closed on June 7, 2022, 12:55pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
