# Shifting TMultigraph on x-axis to the same starting point

**URL:** <https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555>\
**Category:** ROOT\
**Created:** [January 28, 2013, 10:28am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555 "2013-01-28T10:28:34Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@Pree](https://root-forum.cern.ch/u/Pree)\
**Post date:** [January 28, 2013, 10:28am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/1 "2013-01-28T10:28:34Z")

</div>

Hello,

I want to shift TGraph to zero on the X-Axis. I am looking for a solution, where all the graphs should start from same point. Please find the attachment for more clarification of the problem.

My code is as follows:

```
     TFile *f1 = TFile::Open("graph.root");
 
 TCanvas *c = new TCanvas("S0", "S0", 0, 0, 1000, 1000);
 TTree *T1 = (TTree*)f1->Get("abc");

  	 TCut cut = "Time>3";  
             		 
	 T1->Draw("a0:t", cut, "same");  
	 TGraph *gr1 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());  
	 gr1->SetLineColor(kRed);
	 
	 T1->Draw("a1:t", cut, "same");  
	 TGraph *gr2 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());  
	 gr2->SetLineColor(kBlue);
	 
	 T1->Draw("a2:t", cut, "same");  
	 TGraph *gr3 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());  
	 gr3->SetLineColor(kMagenta);
	 
	 T1->Draw("a3:t", cut, "same");
             TGraph *gr4 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());  
         gr4->SetLineColor(kBlack);
	 
	 TMultiGraph *mg = new TMultiGraph();
	 mg -> Add(gr1);
	 mg -> Add(gr2);
	 mg -> Add(gr3);
	 mg -> Add(gr4);
	 mg -> Draw("AC");
	 [color=#FF0040]mg->GetXaxis()->SetLimits(0, 1500);[/color] //Here I am trying to set the starting point of all the Graph to zero but its not working.

```

OR we have some function for shifiting the TGraph? Guide me.

Thank you.

 ![](https://root-forum.cern.ch/uploads/default/original/2X/2/2b8597b01ca29235a7349c4f6c690411fe083f17.jpg)

---

<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:** [January 28, 2013, 12:49pm UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/2 "2013-01-28T12:49:31Z")

</div>

You should shift all the points of the individual graphs along the X axis. Changing the limits will not help.  
A simple for loop will do it.

---

<div class="post-metadata">

**Author:** ![Pree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@Pree](https://root-forum.cern.ch/u/Pree)\
**Post date:** [January 29, 2013, 8:30am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/3 "2013-01-29T08:30:23Z")

</div>

Thanks for the reply. You mean by using a for loop. I tried it but its not working. Would you explain it in detail?

Thank you.

---

<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:** [January 29, 2013, 9:01am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/4 "2013-01-29T09:01:50Z")

</div>

You want to shift all the graphs at (0.,0.). I guess you want to do that keeping the graphs shapes. Therefore you need to shift all the points of each graph. The pseudo C code would be:

```auto
dx = x[0];
for ( int i = 0; i<number_of_points_in_graph; i++) x[i] = x[i]-dx;
```

---

<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:** [January 29, 2013, 9:03am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/5 "2013-01-29T09:03:36Z")

</div>

`{
  Double_t newXmin = 0.0;
  Double_t oldXmin = TMath::MinElement(MyGraph->GetN(), MyGraph->GetX());
  for (Int_t i = 0; i < MyGraph->GetN(); i++)
    (MyGraph->GetX())[i] = (MyGraph->GetX())[i] - oldXmin + newXmin;
  delete MyGraph->GetHistogram(); MyGraph->SetHistogram(0);
  if (gPad) { gPad->Modified(); gPad->Update(); }
}`

---

<div class="post-metadata">

**Author:** ![Pree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@Pree](https://root-forum.cern.ch/u/Pree)\
**Post date:** [January 29, 2013, 9:34am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/6 "2013-01-29T09:34:11Z")

</div>

Thanks for the reply.

@Wile E. Coyote : I used the tips gave by you in the code. But its not working.

```
T1->Draw("a0:t", cut, "same");
TGraph *gr1 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
       Double_t xmin = TMath::MinElement(gr1 ->GetN(), gr1->GetX());
      for (Int_t i = 0; i < gr1 ->GetN(); i++)
                (gr1 ->GetX())[i] = (gr1 ->GetX())[i] - xmin;

 gr1->SetLineColor(kRed);

```

@ couet : Yes, I want to shift all the graphs to a Starting point (0,0). While keeping the shape of the graph will be the same as previous.  
What you mean by x[0]? Is it array of all x-points in the graph. Means, x[0] = gr1-\>GetX();

Thank you.

---

<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:** [January 29, 2013, 10:01am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/7 "2013-01-29T10:01:07Z")

</div>

yes it is the array you get with gr1-\>GetX();

---

<div class="post-metadata">

**Author:** ![Pree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@Pree](https://root-forum.cern.ch/u/Pree)\
**Post date:** [January 29, 2013, 10:16am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/8 "2013-01-29T10:16:09Z")

</div>

Ok. I used it as follows:

```auto
T1->Draw("a0:t", cut, "same");
TGraph *gr1 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
     Int_t n = gr1->GetN();  
			 Double_t x = gr1->GetX();
			 Double_t y = gr1->GetY();
			 for (Int_t i = 0; i < n; i++) 
			 {
			     int dx,dy;
			     dx = x[0];
			     dy = y[0];
              		     x[i] = x[i]-dx;
			     y[i] = y[i]-dy;
			     gr1->SetPoint(i,x,y); //Is it correct? I have it because,in the code we are not setting the updated values
			 }    
  gr1->SetLineColor(kRed);
```

But its not working. I am still looking for the solution.

Thank you.

---

<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:** [January 29, 2013, 10:20am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/9 "2013-01-29T10:20:42Z")

</div>

But you do realise that you need to “shift” each of your graphs (i.e. one by one) and that for each of them you need to calculate its own “oldXmin”, don’t you?

---

<div class="post-metadata">

**Author:** ![Pree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@Pree](https://root-forum.cern.ch/u/Pree)\
**Post date:** [January 29, 2013, 10:30am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/10 "2013-01-29T10:30:57Z")

</div>

Yes, I am calculating xmin and ymin values for each graph still there is no difference in the Graph.

---

<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:** [January 29, 2013, 10:34am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/11 "2013-01-29T10:34:30Z")

</div>

Ok I guess, instead of trying to explain, a working example is better. Here it is:

```auto
void shiftgraph() {
   
   TCanvas *c1 = new TCanvas("c1","A Simple Shift Graph Example",200,10,700,500);

   // Create a simple graph
   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)-5;
   }
   gr = new TGraph(n,x,y);
   
   // now shift it
   
   Double_t dx = 10.;
   Double_t *xs = gr->GetX();
   for (Int_t i=0;i<n;i++) xs[i] = xs[i]-dx;
   
   
   gr->Draw("A*L");
}

```

---

<div class="post-metadata">

**Author:** ![Pree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@Pree](https://root-forum.cern.ch/u/Pree)\
**Post date:** [January 29, 2013, 10:56am UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/12 "2013-01-29T10:56:04Z")

</div>

Ok. We have to assign dx value. Yes, now it is working.  
Thanks a lot.

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [January 29, 2013, 9:13pm UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/13 "2013-01-29T21:13:08Z")

</div>

I’m actually having the same issue, but wanting to re-scale the X axis of a TGraph. I am using PyROOT and I am wondering why the obvious solution is not working:

```auto
x = g.GetX()
for i in range(N):
    x *= scaling
g.Draw("AL")
```

It looks like the x values have changed, but now it does not draw properly. Is there anything else I need to do manually to make it draw properly? I tried g.GetXaxis().SetRangeUser(), but it makes no difference. The axis that shows up is always the original one, so the new graph is off-scale.

Jean-François

---

<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:** [January 29, 2013, 9:15pm UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/14 "2013-01-29T21:15:56Z")

</div>

Try: `delete MyGraph->GetHistogram(); MyGraph->SetHistogram(0);
if (gPad) { gPad->Modified(); gPad->Update(); }`

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [January 29, 2013, 10:15pm UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/15 "2013-01-29T22:15:06Z")

</div>

Thanks, that works. I guess it’s because the TGraph is somehow converted to a histogram when drawn with the “A” option, but then that histogram sticks around?

---

<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:** [January 29, 2013, 10:17pm UTC](https://root-forum.cern.ch/t/shifting-tmultigraph-on-x-axis-to-the-same-starting-point/15555/16 "2013-01-29T22:17:58Z")

</div>

The “A” drawing option creates a “dummy” histogram which is used to draw axes. Once it exists, successive calls to “Draw” will not “recreate” / “renew” it.  
See also: [TGraph::GetHistogram](http://root.cern.ch/root/html/TGraph.html#TGraph:GetHistogram)
