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) 看不见??
利用透明画布
利用缩放
反轴画法
参考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轴为对数轴,不含参数默认设置对数轴。
时间轴画法
极坐标轴画法
参考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
Last updated