Dear co-ROOTers,
I am trying to plot 2 TGraphs (in root 5.34) that share the same x-axis range whereas the y-axis scale is a bit different.
I have found a way to do it by creating a TCanvas and then two TPads, the second of which is transparent. The problem with this solution is that I am not able to modify (change color, make a fit, show confidence bands of the fit etc.) the TGraph that is drawn on the first TPad through the editor (for educational purposes it has to be like that )
Another idea would be to use a TMultiGraph, so as to avoid creating two pads. However when doing the TMultiGraph I am not able to individually play with the ranges of the TGraphs, so only the axes of one of the graphs are kept despite trying to add with their own ranges using
mg->Add(g1, "AP");
mg->Add(g2, "APY+");
So in this case only the axes of the first TGraph are drawn
Any idea how to draw both graphs on the same canvas, at two different y-axes and be able to edit the canvas by hand on not by code?
Thanks in advance!
My code is the following
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <vector>
#include "TGraph.h"
#include "TMultiGraph.h"
#include "TGraphErrors.h"
#include "TGaxis.h"
#include "TAxis.h"
#include "TCanvas.h"
using namespace std;
int two_axes(){
//Define variables
int n = 26;
float x[n], y1[n], y2[n], dy2[n];
float X, Y1, Y2, dY2;
//Read the ascii file that contains the data
std::ifstream file ("ECR_TCRMHz_fDT_dfDT.dat");
if(!file){
cout << "File doesn't exist" << endl;
}
int i = 0;
while(!file.eof()){
file >> X >> Y1 >> Y2 >> dY2;
x[i]=X;
y1[i]=Y1;
y2[i]=Y2;
dy2[i]=dY2;
i++;
}
//Create the graphs
TGraph* g1 = new TGraph(n,x,y1);
TGraphErrors* g2 = new TGraphErrors(n,x,y2,NULL,dy2);
TMultiGraph* mg = new TMultiGraph();
//Make them look aesthetically better
// g1
g1->SetMarkerStyle(20);
g1->SetMarkerSize(2.);
//g2
g2->SetMarkerStyle(21);
g2->SetMarkerSize(2.);
g2->SetMarkerColor(kBlue);
g2->SetLineColor(kBlue);
//Create the TCanvas
TCanvas *c1 = new TCanvas("c1","c1",600,600);
c1->cd();
TPad *pad1 = new TPad("pad1","",0,0,1,1);
TPad *pad2 = new TPad("pad2","",0,0,1,1);
pad2->SetFillStyle(4000); //will be transparent
pad2->SetFrameFillStyle(0);
//Draw the first graph
//pad1->Draw();pad1->cd();
//g1->Draw("AP");
g1->GetXaxis()->SetRangeUser(0,1200);
g1->GetYaxis()->SetRangeUser(0,2);
//Define an axis to draw the second graph
//TGaxis *axis = new TGaxis(1200,0,1200,2,1,1.8,510,"+L");
//axis->SetLabelColor(kBlue);
//axis->Draw();
//Draw the second graph
//pad2->Draw();pad2->cd();
//g2->Draw("APY+");
g2->GetYaxis()->SetRangeUser(1,1.8);
g2->GetXaxis()->SetRangeUser(0,1200);
//Add graphs in the multigraph
mg->Add(g1, "AP");
mg->Add(g2, "APY+");
mg->Draw("AP");
c1->Update();
//c1->SaveAs("/eos/user/a/astamato/Forums/RootForum/c1.root");
return 0;
}
To reproduce the problem, here is a link to the data file
39.31 0.04 1.0175 0.00102
58.47 0.06 1.0262 0.00103
77.28 0.08 1.0352 0.00104
95.80 0.10 1.0438 0.00104
114 0.12 1.0527 0.00105
131.88 0.14 1.0615 0.00106
149.62 0.16 1.0693 0.00107
166.96 0.18 1.0781 0.00108
184.07 0.20 1.0865 0.00109
266.29 0.30 1.1366 0.00113
343.25 0.40 1.1753 0.00117
415.76 0.50 1.2126 0.0012
485.16 0.60 1.2601 0.00124
538.83 0.70 1.29001 0.0013
603.77 0.80 1.3250 0.00133
654.29 0.90 1.3655 0.00138
700.22 1.00 1.3805 0.00144
768.05 1.10 1.4322 0.00143
813.48 1.20 1.4751 0.00148
864.75 1.30 1.4953 0.0015
912.85 1.40 1.5337 0.00153
957.71 1.50 1.5662 0.00157
1003.04 1.60 1.5951 0.0016
1049.99 1.70 1.6191 0.00162
1095.43 1.80 1.6500 0.00164
1141.19 1.90 1.6749 0.00166