TGraph with 2 right and left axis

Hallo Dear Root expert,

I try to plot 2 TGraphs on the same TCanvas. As they have different scales, I use two TPad (the second being transparent) as explained in transpad.C, but with TGraph instead of histograms. It is working fine, except the margins are outside the pads (we don’t see the labels and title of the TGraph’s). As once plotted, we can only access graphically the second pad, I can not resize anything by hand. So I tried several things in the macro (playing with TCanvas, TPad, TFrame, etc). I found the command pad->SetLeftMargin(…), and it works for resizing the graphs inside the pads such that we see the labels, but now the additional left-side axis (put by hand at the end of the macro) does not scale (and I can not do it manually)

Here is the macro

===========================
TCanvas *c1 = new TCanvas(“c1”,“transparent pad”,200,10,700,500);
TPad *pad1 = new TPad(“pad1”,"",0,0,1,1);
TPad *pad2 = new TPad(“pad2”,"",0,0,1,1);
pad2->SetFillStyle(4000);

   pad1->SetLeftMargin(0.2);  // ======= THIS IS WHAT I HAVE ADDED 
   pad1->SetBottomMargin(0.2);
   pad1->SetRightMargin(0.2);       

   pad1->Draw();
   pad1->cd();  

   TH1F *hr = c1->DrawFrame(0., 0., 2.5, 0.1);

   hr->SetXTitle("p_{T} [GeV / c]");
   hr->GetXaxis()->CenterTitle();
   hr->GetXaxis()->SetTitleSize(0.06);
   hr->GetXaxis()->SetTitleOffset(1.2);
   hr->GetXaxis()->SetLabelSize(0.06);
   hr->GetXaxis()->SetLabelOffset(0.02);

   hr->SetYTitle("v_{2}");
   hr->GetYaxis()->CenterTitle();
   hr->GetYaxis()->SetTitleSize(0.06);
   hr->GetYaxis()->SetTitleOffset(1.3);
   hr->GetYaxis()->SetLabelSize(0.06);
   hr->GetYaxis()->SetLabelOffset(0.02);

   gr1 = new TGraphErrors(n, ..., ..., ..., ...); // ======== I SIMPLIFIED HERE
   gr1->SetMarkerColor(kBlack);
   gr1->SetMarkerStyle(20);
   gr1->SetMarkerSize(2.);
   gr1->SetLineWidth(4);
   gr1->Draw("P");  

   pad1->Update(); //this will force the generation of the "stats" box === I DON'T NEED STATS BOX
   c1->cd();       

   //compute the pad range with suitable margins
   Double_t ymin = 0;
   Double_t ymax = 60;
   Double_t dy = (ymax-ymin)/0.8; //10 per cent margins top and bottom
   Double_t xmin = 0;
   Double_t xmax = 2.5;
   Double_t dx = (xmax-xmin)/0.8; //10 per cent margins left and right
   pad2->Range(xmin-0.1*dx,ymin-0.1*dy,xmax+0.1*dx,ymax+0.1*dy);

   pad2->SetLeftMargin(0.2);
   pad2->SetBottomMargin(0.2);
   pad2->SetRightMargin(0.2);
   
   pad2->Draw();
   pad2->cd();
   
   gr2 = new TGraphErrors(n,..., ..., ..., ...); // ======= I SIMPLIFIED HERE
   gr2->SetMarkerColor(kRed);
   gr2->SetMarkerStyle(20);
   gr2->SetMarkerSize(2.);
   gr2->SetLineWidth(4);
   gr2->Draw("P");  //LP

   // draw axis on the right side of the pad
   TGaxis *axis = new TGaxis(xmax,ymin+3.8,xmax,ymax-3.8,ymin,ymax,50510,"+L");
   axis->SetLabelColor(kRed);
   axis->SetTitle("#delta v_{2} / v_{2} [%]");
   axis->CenterTitle();
   axis->SetTitleSize(0.06);
   axis->SetTitleColor(kRed);
   axis->SetTitleOffset(1.2);
   axis->SetLabelSize(0.06);
   axis->SetLabelOffset(0.02);
   axis->Draw();

=========================================

Do you have a solution? Thank you very much by advance.

Best regards, Selim

I get many erros with you macro. Do you get them too ?

root [0] 
Processing selim.C...
Warning in <TCanvas::DrawFrame>: Drawframe must be called for the current pad only
Error: Symbol n is not defined in current scope  selim.C:30:
Warning: Automatic variable gr1 is allocated selim.C:30:
Error: Undeclared variable gr1 selim.C:30:
*** Interpreter error recovered ***
root [1] 

Ho sorry, I didn’t add the input vectors for TGraphError’s :

{
Int_t n = 4;
Double_t pt[4] = {0.32, 0.74, 1.2, 1.8};
Double_t v2[4] = {0.005, 0.0134, 0.0316, 0.0738};
Double_t errRel[4] = {285, 86, 49, 33};
Double_t zero[4] = {0., 0., 0., 0.}; 

TCanvas *c1 = new TCanvas("c1","transparent pad",200,10,700,500);
TPad *pad1 = new TPad("pad1","",0,0,1,1);
TPad *pad2 = new TPad("pad2","",0,0,1,1);
pad2->SetFillStyle(4000);

pad1->SetLeftMargin(0.2); // ======= THIS IS WHAT I HAVE ADDED
pad1->SetBottomMargin(0.2);
pad1->SetRightMargin(0.2);

pad1->Draw();
pad1->cd();

TH1F *hr = c1->DrawFrame(0., 0., 2.5, 0.1);

hr->SetXTitle("p_{T} [GeV / c]");
hr->GetXaxis()->CenterTitle();
hr->GetXaxis()->SetTitleSize(0.06);
hr->GetXaxis()->SetTitleOffset(1.2);
hr->GetXaxis()->SetLabelSize(0.06);
hr->GetXaxis()->SetLabelOffset(0.02);

hr->SetYTitle("v_{2}");
hr->GetYaxis()->CenterTitle();
hr->GetYaxis()->SetTitleSize(0.06);
hr->GetYaxis()->SetTitleOffset(1.3);
hr->GetYaxis()->SetLabelSize(0.06);
hr->GetYaxis()->SetLabelOffset(0.02);

gr1 = new TGraphErrors(n, pt, v2, zero, zero); // ======== I SIMPLIFIED HERE
gr1->SetMarkerColor(kBlack);
gr1->SetMarkerStyle(20);
gr1->SetMarkerSize(2.);
gr1->SetLineWidth(4);
gr1->Draw("P");

pad1->Update(); //this will force the generation of the "stats" box === I DON'T NEED STATS BOX
c1->cd();

//compute the pad range with suitable margins
Double_t ymin = 0;
Double_t ymax = 60;
Double_t dy = (ymax-ymin)/0.8; //10 per cent margins top and bottom
Double_t xmin = 0;
Double_t xmax = 2.5;
Double_t dx = (xmax-xmin)/0.8; //10 per cent margins left and right
pad2->Range(xmin-0.1*dx,ymin-0.1*dy,xmax+0.1*dx,ymax+0.1*dy);

pad2->SetLeftMargin(0.2);
pad2->SetBottomMargin(0.2);
pad2->SetRightMargin(0.2);

pad2->Draw();
pad2->cd();

gr2 = new TGraphErrors(n,pt, errRel, zero, zero); // ======= I SIMPLIFIED HERE
gr2->SetMarkerColor(kRed);
gr2->SetMarkerStyle(20);
gr2->SetMarkerSize(2.);
gr2->SetLineWidth(4);
gr2->Draw("P"); //LP

// draw axis on the right side of the pad
TGaxis *axis = new TGaxis(xmax,ymin+3.8,xmax,ymax-3.8,ymin,ymax,50510,"+L");
axis->SetLabelColor(kRed);
axis->SetTitle("#delta v_{2} / v_{2} [%]");
axis->CenterTitle();
axis->SetTitleSize(0.06);
axis->SetTitleColor(kRed);
axis->SetTitleOffset(1.2);
axis->SetLabelSize(0.06);
axis->SetLabelOffset(0.02);
axis->Draw();
}

It should work now … hopefully. Thanks!

In fact I had an example doing this. Here it is:

{
   //
   // Example showing how to superimpose a TGraph with a different range
   // using a transparent pad.
   //

   gROOT->ForceStyle(0);
   c1 = new TCanvas("c1","gerrors2",200,10,700,500);
   TPad *pad = new TPad("pad","",0,0,1,1);
   pad->SetFillColor(42);
   pad->SetGrid();
   pad->Draw();
   pad->cd();

      // draw a frame to define the range
   TH1F *hr = c1->DrawFrame(-0.4,0,1.2,12);
   hr->SetXTitle("X title");
   hr->SetYTitle("Y title");
   pad->GetFrame()->SetFillColor(21);
   pad->GetFrame()->SetBorderSize(12);

      // create first graph
   Int_t n1 = 10;
   Double_t x1[]  = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};
   Double_t y1[]  = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};
   Double_t ex1[] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05};
   Double_t ey1[] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8};
   gr1 = new TGraphErrors(n1,x1,y1,ex1,ey1);
   gr1->SetMarkerColor(kBlue);
   gr1->SetMarkerStyle(21);
   gr1->Draw("LP");

      // create second graph
   Int_t n2 = 10;
   Float_t x2[]  = {-0.28, 0.005, 0.19, 0.29, 0.45, 0.56,0.65,0.80,0.90,1.01};
   Float_t y2[]  = {0.82,3.86,7,9,10,10.55,9.64,7.26,5.42,2};
   Float_t ex2[] = {.04,.12,.08,.06,.05,.04,.07,.06,.08,.04};
   Float_t ey2[] = {.6,.8,.7,.4,.3,.3,.4,.5,.6,.7};
   for (Int_t i=0;i<n2;i++) {
      y2[i] *= 100;
      ey2[i] *= 100;
   }
   //create a transparent pad drawn on top of the main pad
   c1->cd();
   TPad *overlay = new TPad("overlay","",0,0,1,1);
   overlay->SetFillStyle(0);
   overlay->SetFillColor(0);
   overlay->SetFrameFillStyle(0);
   overlay->Draw("FA");
   overlay->cd();
   gr2 = new TGraphErrors(n2,x2,y2,ex2,ey2);
   gr2->SetMarkerColor(kRed);
   gr2->SetMarkerStyle(20);
   gr2->SetName("gr2");
   Double_t xmin = pad->GetUxmin();
   Double_t ymin = 0;
   Double_t xmax = pad->GetUxmax();
   Double_t ymax = 1300;
   TH1F *hframe = overlay->DrawFrame(xmin,ymin,xmax,ymax);
   hframe->GetXaxis()->SetLabelOffset(99);
   hframe->GetYaxis()->SetTickLength(0);
   hframe->GetYaxis()->SetLabelOffset(99);
   gr2->Draw("LP");


   //Draw an axis on the right side
   TGaxis *axis = new TGaxis(xmax,ymin,xmax, ymax,ymin,ymax,510,"+L");
   axis->SetLineColor(kRed);
   axis->SetLabelColor(kRed);
   axis->Draw();

}

Thank you sooooooooooo much!!!

Works fine, and now I can use SetLeftMargin(), etc, without having scaling problems for the second axis. Really helpful :wink: