使用“用户坐标”而不是 bin 编号来设置范围更方便。且允许用户直接在直方图对象上TH::SetAxisRange(min, max, "axis")设置轴范围,而无需使用Get[XYZ]axis()。E.g.
hpxpy->GetYaxis()->SetRangeUser(0.,3.);
等价于:
hpxpy->SetAxisRange(0.,3.,"Y");
支持绘制图形后更改轴范围:
{auto c =newTCanvas("c","A Zoomed Graph",200,10,700,500);int n =10;doublex[10] = {-.22,.05,.25,.35,.5,.61,.7,.85,.89,.95};doubley[10] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};auto gr =newTGraph(n,x,y);gr->SetMarkerColor(4);gr->SetMarkerStyle(20);gr->Draw("ALP");gr->GetXaxis()->SetRangeUser(0,0.5);gr->GetYaxis()->SetRangeUser(1,8);}
{auto c =newTCanvas("c","A Zoomed Graph",200,10,700,500);c->DrawFrame(0,1,0.5,8);int n =10;doublex[10] = {-.22,.05,.25,.35,.5,.61,.7,.85,.89,.95};doubley[10] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};auto gr =newTGraph(n,x,y);gr->SetMarkerColor(4);gr->SetMarkerStyle(20);gr->Draw("LP");}
voidtranspad(){ 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);pad1->Draw();pad2->SetFillStyle(4000); //will be transparentpad1->cd(); TH1F *h1 = new TH1F("h1","h1",100,-3,3); TH1F *h2 = new TH1F("h2","h2",100,-3,3); TRandom r;for (Int_t i=0;i<100000;i++) {Double_t x1 =r.Gaus(-1,0.5);Double_t x2 =r.Gaus(1,1.5);if (i <1000) h1->Fill(x1);h2->Fill(x2); }h1->Draw();pad1->Update(); //this will force the generation of the "stats" box TPaveStats *ps1 = (TPaveStats*)h1->GetListOfFunctions()->FindObject("stats");ps1->SetX1NDC(0.4); ps1->SetX2NDC(0.6);pad1->Modified();c1->cd();//compute the pad range with suitable marginsDouble_t ymin =0;Double_t ymax =2000;Double_t dy = (ymax-ymin)/0.8; //10 per cent margins top and bottomDouble_t xmin =-3;Double_t xmax =3;Double_t dx = (xmax-xmin)/0.8; //10 per cent margins left and rightpad2->Range(xmin-0.1*dx,ymin-0.1*dy,xmax+0.1*dx,ymax+0.1*dy);pad2->Draw();pad2->cd();h2->SetLineColor(kRed);// h2->Draw("][sames");h2->Draw("sames");pad2->Update(); TPaveStats *ps2 = (TPaveStats*)h2->GetListOfFunctions()->FindObject("stats");ps2->SetX1NDC(0.65); ps2->SetX2NDC(0.85);ps2->SetTextColor(kRed);// draw axis on the right side of the pad TGaxis *axis = new TGaxis(xmax,ymin,xmax,ymax,ymin,ymax,50510,"+L");axis->SetLabelColor(kRed);axis->Draw();}
显示两个不同比例的直方图的画布示例-利用缩放
#include "TCanvas.h"
#include "TStyle.h"
#include "TH1.h"
#include "TGaxis.h"
#include "TRandom.h"
void twoscales()
{
TCanvas *c1 = new TCanvas("c1","hists with different scales",600,400);
//create/fill draw h1
gStyle->SetOptStat(kFALSE);
TH1F *h1 = new TH1F("h1","my histogram",100,-3,3);
Int_t i;
for (i=0;i<10000;i++) h1->Fill(gRandom->Gaus(0,1));
h1->Draw();
c1->Update();
//create hint1 filled with the bins integral of h1
TH1F *hint1 = new TH1F("hint1","h1 bins integral",100,-3,3);
Float_t sum = 0;
for (i=1;i<=100;i++) {
sum += h1->GetBinContent(i);
hint1->SetBinContent(i,sum);
}
//scale hint1 to the pad coordinates
Float_t rightmax = 1.1*hint1->GetMaximum();
Float_t scale = gPad->GetUymax()/rightmax;
hint1->SetLineColor(kRed);
hint1->Scale(scale);
hint1->Draw("same");
//draw an axis on the right side
TGaxis *axis = new TGaxis(gPad->GetUxmax(),gPad->GetUymin(),
gPad->GetUxmax(), gPad->GetUymax(),0,rightmax,510,"+L");
axis->SetLineColor(kRed);
axis->SetLabelColor(kRed);
axis->Draw();
}
voidgaxis(){ auto c1 = new TCanvas("c1","Examples of TGaxis",10,10,700,500);c1->Range(-10,-1,10,1); auto axis1 = new TGaxis(-4.5,-0.2,5.5,-0.2,-6,8,510,"");axis1->Draw(); auto axis2 = new TGaxis(-4.5,0.2,5.5,0.2,0.001,10000,510,"G");axis2->Draw(); auto axis3 = new TGaxis(-9,-0.8,-9,0.8,-8,8,50510,"");axis3->SetTitle("axis3");axis3->SetTitleOffset(0.5);axis3->Draw(); auto axis4 = new TGaxis(-7,-0.8,-7,0.8,1,10000,50510,"G");axis4->SetTitle("axis4");axis4->Draw(); auto axis5 = new TGaxis(-4.5,-0.6,5.5,-0.6,1.2,1.32,80506,"-+");axis5->SetLabelSize(0.03);axis5->SetTextFont(72);axis5->Draw(); auto axis6 = new TGaxis(-4.5,0.5,5.5,0.5,100,900,50510,"-");axis6->Draw(); auto axis7 = new TGaxis(-5.5,0.85,5.5,0.85,0,4.3e-6,510,"");axis7->Draw(); auto axis8 = new TGaxis(8,-0.8,8,0.8,0,9000,50510,"+L");axis8->Draw();// One can make a vertical axis going top->bottom. However the two x values should be// slightly different to avoid labels overlapping. auto axis9 = new TGaxis(6.5,0.8,6.499,-0.8,0,90,50510,"-");axis9->Draw();}
断轴画法
共用轴/拼接轴画法
共轴画法是一种常见的绘图技巧,在同一张 canvas 中绘制多图并共享 X 轴或 Y 轴。这种方式常用于比较不同数据集之间的趋势或关系。最简单的共轴画法是TCanvas::Divide(numberx, numbery, 0)。
voidseism(){//TStopwatch sw;sw.Start();//TDatime dtime;//gStyle->SetTimeOffset( dtime.Convert() ); // replace axis with timeaxis//cout << dtime.Convert() << endl; TCanvas *c1 = new TCanvas("seism","Time on axis",0,0,1000,500); // TCanvas("canvas name","canvas title",the pixel coordinates of the top left corner of canvas,same as above,size of pixel along X,size of pixel along Y)
c1->SetFillColor(42); // fill background colorc1->SetFrameFillColor(33); // color in framec1->SetGrid(); // add grid in the frameFloat_t bintime =1; // set one bin = 1 second. change it to set time scale,don't chenge real time TH1F *ht = new TH1F("ht","The ROOT seism",10,0,10*bintime);// TH1 = 1D histogram;F = float;("name","title",bin,Xmin,Xmax)Float_t signal =700;// c/c++ : int , float , double// POOT :int_t , float_t , double_tht->SetMaximum( signal );ht->SetMinimum(-signal );ht->SetStats(1); // set statistic pad,1=display,0=hideht->SetLineColor(2);ht->GetXaxis()->SetTimeDisplay(1); // settimedisplay,1=display,0=hideht->GetYaxis()->SetNdivisions(220); // n1n2n3,n1 = a bin in the bin;n2n3 = ???ht->Draw();for(int i=0;i<200;i++){// build a signal ; noisy damped sineFloat_t noise =gRandom->Gaus(0,120);if(i>100){ noise = noise +sin((i-700)*6.28/30)*exp((700-i)/300); }ht->SetBinContent(i,noise); // the ith content is noisec1->Modified();c1->Update();gSystem->ProcessEvents(); }//printf("real time =%8.3fs,cpu time =%8.3fsn", sw.RealTime(), sw.CpuTime());return0;}
{ auto c = new TCanvas("c","c",0,0,500,500);c->Range(-11,-11,11,11); auto f2 = new TF1("x2","x*x",-10,10);f2->SetLineColor(kRed);f2->Draw("same"); auto f3 = new TF1("x3","x*x*x",-10,10);f3->SetLineColor(kBlue);f3->Draw("same");// Draw the axis with arrows auto ox = new TGaxis(-10,0,10,0,-10.,10.,510,"+-S>");ox->SetTickSize(0.009);ox->SetLabelFont(42);ox->SetLabelSize(0.025);ox->Draw(); auto oy = new TGaxis(0,-10,0,10,-10,10,510,"+-S>");oy->SetTickSize(0.009);oy->SetLabelFont(42);oy->SetLabelSize(0.025);oy->Draw();}
画面留白
c1 = new TCanvas("c1","A Simple Graph ",300,0,750,560); c1->SetLeftMargin(0.12);, c1->SetBottomMargin(0.10);