Shifting TMultigraph on x-axis to the same starting point

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.


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.

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.

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:

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

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

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.

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

Ok. I used it as follows:

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.

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?

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

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

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");
}
1 Like

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

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:

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

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

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?

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