TGraph的Draw()函数的参数TGraphPainter Class Reference
TGraphError在TGraph中标出点(x,y)的误差($\sigma_x,\sigma_y$)
%jsroot on
TCanvas *c1 = new TCanvas;//创建一个新的画布(Canvas), ROOT中所有的图形对象都画在TCanvas上。
Double_t x[100], y[100];
Int_t n = 20;
for (Int_t i=0;i<n;i++) {
x[i] = i*0.1;
y[i] = 10*sin(8*x[i]+0.2);
}
TGraph* gr1 = new TGraph(n,x,y); //创建TGraph,n-数据点个数,x,y-数组
gr1->SetMarkerStyle(0);//TGraph中画点 0-dot
gr1->Draw("ALP");//画TGraph,P-点的形状,当前的设定为dot,L-点之间用直线连接。
c1->Draw();//画出TCanvas。
!cat data2.txt
# data2.txt 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100
c1->Clear();
TGraph* gr2 = new TGraph("data2.txt");
gr2->SetMarkerStyle(0);//dot
gr2->Draw("ALP");
c1->Draw();
c1->Clear();
TGraph* gr3 = new TGraph();
int j=0;
for(int i=0;i<10;i++){
if(i%2==0) gr3->SetPoint(j++,x[i],y[i]);
}
gr3->SetMarkerStyle(0);//dot
gr3->Draw("ALP");
c1->Draw();
cout<<"linear interpolation of x=0.3: "<<gr3->Eval(0.3,0,"")<<endl;
cout<<"spline interpolation of x=0.3: "<<gr3->Eval(0.3,0,"S")<<endl;
linear interpolation of x=0.3: 3.59153 spline interpolation of x=0.3: 4.34646
TGraph *gr4=new TGraph;
TGraph *gr5=new TGraph;
for(int i=0;i<80;i++) {
double x=i*0.01;
double y1=gr3->Eval(x,0,"");
double y2=gr3->Eval(x,0,"S");
gr4->SetPoint(i,x,y1);
gr5->SetPoint(i,x,y2);
}
c1->Clear();
gr4->SetMarkerStyle(0);//dot
gr4->Draw("ALP");
c1->Draw();
c1->Clear();
gr5->SetMarkerStyle(0);//dot
gr5->Draw("ALP");
c1->Draw();
// 获得 TGraph 填充的数据点个数
cout<< gr2->GetN() <<endl;
// 方法 1
double tmpx, tmpy;
for(int i = 0; i < gr2->GetN(); i++)
{
gr2->GetPoint(i, tmpx,tmpy);
cout<<i<<" "<<tmpx<<" "<<tmpy<<endl;
}
// 方法 2
for(int i = 0; i < gr2->GetN(); i++)
cout<<i<<" "<<gr2->GetX()[i]<<" "<<gr2->GetY()[i]<<endl;
10 0 1 1 1 2 4 2 3 9 3 4 16 4 5 25 5 6 36 6 7 49 7 8 64 8 9 81 9 10 100 0 1 1 1 2 4 2 3 9 3 4 16 4 5 25 5 6 36 6 7 49 7 8 64 8 9 81 9 10 100
TGraph *gr20 = new TGraph;
for(int i = 0; i < gr2->GetN(); i++)
gr20->SetPoint(i, gr2->GetY()[i], gr2->GetX()[i]);
gr20->Draw("APC*");
c1->Draw();
//c1->Clear();
gr1->Draw("APC");
gr2->Draw("PLsame");
c1->Draw();
c1->Clear();
gr2->Draw("APL");
gr1->Draw("PLsame");
c1->Draw();
c1->Clear();
gr1->Draw("APL");
gr2->Draw("PLsame");
// 重新调整 X 坐标范围的方法
gr1->GetXaxis()->SetLimits(-1,11);
// 重新调整 Y 坐标范围的方法
gr1->SetMaximum(110);
gr1->SetMinimum(-20);//0.0001 0.01 0.1
c1->Draw();
const Int_t nr = 10;
Float_t xx[nr] = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};//x
Float_t yy[nr] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};//y
Float_t ex[nr] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05};//sigma_x
Float_t ey[nr] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8};//sigma_y
TGraphErrors *gr = new TGraphErrors(nr,xx,yy,ex,ey);//声明TGraphError,指定点的数目,以及(x,y),(sigma_x,sigma_y)
gr->SetTitle("TGraphErrors Example");
gr->SetMarkerColor(4);
gr->SetMarkerStyle(21);
gr->Draw("ALP");
gr->Fit("pol1");// 一次函数拟合采用参数 pol1 二次函数拟合采用参数 pol2
c1->Draw();
FCN=193.154 FROM MIGRAD STATUS=CONVERGED 107 CALLS 108 TOTAL EDM=5.65891e-13 STRATEGY= 1 ERROR MATRIX ACCURATE EXT PARAMETER STEP FIRST NO. NAME VALUE ERROR SIZE DERIVATIVE 1 p0 3.50428e+00 5.92689e-01 1.46027e-03 4.61690e-06 2 p1 6.38827e+00 9.88411e-01 2.43518e-03 2.97224e-06