2.3.2 坐标轴属性

坐标轴的概念

坐标轴由轴线(axisLine)、刻度(Ticket)、刻度标签(Label)、轴标题(Title)四个部分组成。

root [0] TAxis::Set
SetAlphanumeric
SetAxisColor
SetBinLabel
SetBit
SetCanExtend
SetDecimals
SetDefaults
SetDrawOption
SetDtorOnly
SetLabelColor            // 设置刻度标签颜色
SetLabelFont             // 设置刻度标签字体
SetLabelOffset           // 设置刻度标签偏移量,
SetLabelSize             // 设置刻度标签大小
SetLimits
SetMaxDigits
SetMoreLogLabels
SetName
SetNameTitle
SetNdivisions
SetNoAlphanumeric
SetNoExponent
SetObjectStat
SetParent
SetRange
SetRangeUser
SetTickLength
SetTickSize
SetTicks
SetTimeDisplay
SetTimeFormat
SetTimeOffset
SetTitle
SetTitleColor
SetTitleFont
SetTitleOffset
SetTitleSize
SetUniqueID

坐标轴的轴线

颜色:TStyle::SetAxisColor(,) E.g. gStyle->SetAxisColor(2,"xy")设置 x、y 轴为红色。

坐标轴的标题

标题支持 LaTeX 语法。

设置标题的方法是GetX/Yaxis->SetTitle("name"),也可以同时设置标题、x轴标题、y轴标题方法是SetTitle("graph tittle; x tittle; y tilttle")

同时标题支持自定义:

颜色:SetTitleColor()

大小:SetTitleSize()

字体:SetTextFont()

距离:SetTitleOffset() 设置轴和轴标题之间的距离。

位置:CenterTitle(true)

偏移量=1使用标准位置,该位置根据标签偏移量和尺寸计算;偏移量=1.2将使标准偏移量增加20%。

hist->GetXaxis()->SetRange(0.02);

hist->GetYaxis()->SetLabelOffset(0.02);

标题偏移量

hist->GetXaxis()->SetLabelOffset(0.02);

hist->GetYaxis()->SetLabelOffset(0.02);

坐标轴的范围

合理控制的轴范围,能够更好表达数据变化。root 可以自动选取轴的范围,如果自动的呈现效果不满意,root 也支持手动调整。

对于未分 bin 的数据,例如图表,更改轴范围使用TAxis::SetLimits(xmin, xmax)函数;

而直方图轴范围使用TAxis::SetRange(number bin1, number bin2)函数修改,参数是分 bin 的编号的位置:

{
   TFile f("hsimple.root");
   hpxpy->Draw("colz");
   hpxpy->GetYaxis()->SetRange(22,23);
   hpxpy->GetXaxis()->SetRange(18,19);
}

使用“用户坐标”而不是 bin 编号来设置范围更方便。且允许用户直接在直方图对象上TH::SetAxisRange(min, max, "axis")设置轴范围,而无需使用Get[XYZ]axis()。E.g.

   hpxpy->GetYaxis()->SetRangeUser(0., 3.);

等价于:

   hpxpy->SetAxisRange(0., 3.,"Y");

支持绘制图形后更改轴范围:

{
   auto c = new TCanvas("c","A Zoomed Graph",200,10,700,500);

   int n = 10;
   double x[10] = {-.22,.05,.25,.35,.5,.61,.7,.85,.89,.95};
   double y[10] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};

   auto gr = new TGraph(n,x,y);
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(20);
   gr->Draw("ALP");
   gr->GetXaxis()->SetRangeUser(0, 0.5);
   gr->GetYaxis()->SetRangeUser(1, 8);
}

也可以绘制图表框架TCanvas::DrawFrame(,,,)。此方法允许指定比原始范围更大的轴范围。E.g.

{
   auto c = new TCanvas("c","A Zoomed Graph",200,10,700,500);
   c->DrawFrame(0,1,0.5,8);

   int n = 10;
   double x[10] = {-.22,.05,.25,.35,.5,.61,.7,.85,.89,.95};
   double y[10] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};

   auto gr = new TGraph(n,x,y);
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(20);
   gr->Draw("LP");
}

调整坐标极值

下限:TGraph::SetMinimum();TH::SetMinimum()

上限:TGraph::SetMaximum();TH::SetMaximum()

坐标轴的刻度

偏移量:TStyle::SetLabelOffset(,"")。偏移量以 pad 高度的百分比表示。 axis 指定哪个轴 ("x","y","z"),如果 axis="xyz" 设置所有 3 个轴,则默认 = "x"。

字体:TStyle::SetLabelFont(,"")

颜色:TStyle::SetLabelColor(,"")

绘制直方图或图表时,可以使用以下命令将网格设置为打开或关闭:

  • gPad->SetGridy(1);设置X轴上的网格

  • gPad->SetGridx(1);设置Y轴上的网格

  • gPad->SetGrid (1,1);在两个轴上设置网格。

坐标轴的标签

标签大小:SetLabelSize(0.05,"xy");设置轴标签的大小。尺寸以Pad高度的百分比表示。 axis 指定哪个轴 ("x","y","z"),如果 axis="xyz" 设置所有 3 个轴,则默认 = "x"

小数对齐:TStyle::SetStripDecimals()。将0,0.5,1.25对齐为0.00,0.50,1.25

TGaxis::SetMaxDigits设置轴标签允许的最大位数。

刻度线TStyle::SetNdivisions()设置绘制轴的分割数。

ndiv :分区数。

n = N1 + 100*N2 + 10000*N3。N1=初级分区的数量。N2=二级划分的数量。N3=第三分区的数量。例如:nndi=0 --> 无刻度线。nndi=2 --> 2 个分区,中间有一个刻度线轴的。

axis 指定哪个轴 ("x","y","z"),如果 axis="xyz" 设置所有 3 个轴,则默认 = "x"

TStyle::SetTickLength

设置轴的刻度线长度。

axis 指定哪个轴 ("x","y","z"),如果 axis="xyz" 设置所有 3 个轴,则默认 = "x"

双轴画法

双轴画法是一种作图技巧,通过在画面中同时运用两个或多个对称轴,以达到对称和平衡或突出、方便对比等效果。最简单的呈现效果是使用gPad

gPad->SetTicks(,)    // 两个参数分别控制x、y轴的对轴
                     // 0 表示不绘制对轴
                     // 1 表示绘制对轴的刻度
                     // 2 表绘制对轴及对应坐标值
                     // 例如gPad->SetTicks(1,2)表示绘制右轴和上轴及上轴对应的坐标值

gPad->SetMargin(10,10,10,10) 看不见??

显示两个不同比例的直方图的画布示例-利用透明Pad
void transpad()
{
	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 transparent
	
	pad1->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 margins
	Double_t ymin = 0;
	Double_t ymax = 2000;
	Double_t dy = (ymax-ymin)/0.8; //10 per cent margins top and bottom
	Double_t xmin = -3;
	Double_t xmax = 3;
	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->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();
}

反轴画法

参考2.3节图形的绘图选项

多轴画法

void gaxis(){
   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)

参考数据可视化多图画法

对数轴画法

对数轴是Tcanvas的类,所以设置对数轴,需要先建立画布。

使用SetLogx()SetLogy()命令来设置画布的X轴和Y轴为对数坐标轴。E.g.

TCanvas *c1 = new TCanvas("c1", "Example", 800, 600);
    c1->SetLogx(0);
    c1->SetLogy(1);
    c1->Update();

SetLogx(0)SetLogy(1)分别表示取消x对数轴和设置y轴为对数轴,不含参数默认设置对数轴。

时间轴画法

E.g. 模拟地震波随时间变化的图案
void seism(){

    //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 color
    c1->SetFrameFillColor( 33 ); // color in frame
    c1->SetGrid();  // add grid in the frame

    Float_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_t

    ht->SetMaximum( signal );
    ht->SetMinimum( -signal );
    ht->SetStats( 1 );  // set statistic pad,1=display,0=hide
    ht->SetLineColor( 2 );
    ht->GetXaxis()->SetTimeDisplay( 1 );    // settimedisplay,1=display,0=hide
    ht->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 sine
        Float_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 noise
        c1->Modified();
        c1->Update();
        gSystem->ProcessEvents();
    }
    
    //printf("real time =%8.3fs,cpu time =%8.3fsn", sw.RealTime(), sw.CpuTime());
    return 0;
}

极坐标轴画法

参考2.3节图形的极坐标图案

笛卡尔坐标系

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

坐标轴网格

颜色:TStyle::SetGridColor()

样式:TStyle::SetGridStyle()

宽度:TStyle::SetGridWidth()

坐标轴边框

与坐标轴对应的位置是边框,TCanvas::????

设置帧边框模式()TStyle::SetFrameBorderMode

设置框架边框大小()无效 TStyle::SetFrameBorderSize

颜色:TStyle::SetFrameFillColor

样式:TStyle::SetFrameFillStyle

颜色:TStyle::SetFrameLineColor

线条:TStyle::SetFrameLineStyle

TStyle::SetFrameLineWidth


参考

https://root.cern/doc/master/classTGaxis.html

https://root-forum.cern.ch/t/using-setlimits-changes-the-histogram/43194/6

https://root.cern.ch/doc/master/classTStyle.html#a906e5f9060357a95f893701b3bed57a2

https://root.cern/doc/v630/classTH1.html#afd36e57d93056e578d73291ae3e1bb01

最后更新于