图例可以用来解释图中的各个部分的名称,他的用法是:TLegend *l = new TLegend(,,,,""),意为新建一个名为l的图例,参数前四位是相对于画布的归一化坐标,(0,0)表示画布的左下角,(1,1)表示画布的右上角。
增加图例内容
使用l->AddEntry(,"")函数为你的图例添加新的解释:
E.g. 介绍四个高斯函数的图例
voidex3_gaus(){ TCanvas *c = new TCanvas("c","Four Gaussian Distributions",600,600);c->SetGrid(); TF1 *f1 = new TF1("f1","TMath::Gaus(x,0,1)",-6,6); TF1 *f2 = new TF1("f2","TMath::Gaus(x,0,2)",-6,6); TF1 *f3 = new TF1("f3","TMath::Gaus(x,1,1)",-6,6); TF1 *f4 = new TF1("f4","TMath::Gaus(x,1,2)",-6,6); TLegend *leg = new TLegend(0.1,0.7,0.4,0.9,"");leg->AddEntry(f1,"Gaus(0,1)");leg->AddEntry(f2,"Gaus(0,2)");leg->AddEntry(f3,"Gaus(1,1)");leg->AddEntry(f4,"Gaus(1,2)");f1->SetLineColor(2);f1->SetLineWidth(4);f1->Draw(); f2->SetLineColor(4);f2->SetLineWidth(4);f2->Draw("same");f3->SetLineColor(6);f3->SetLineWidth(4);f3->Draw("same");f4->SetLineColor(8);f4->SetLineWidth(4);f4->Draw("same");f1->SetTitle("Four Gaussian Distributions");leg->Draw();c->SaveAs("ex3_gaus.pdf");c->SaveAs("ex3_gaus.svg");}
修改图例框
使用SetBorderSize(0)命令关闭图例框在某些情况下图像更加美观
添加阴影
修改位置
设置多列:leg->SetNColumns (2);
画布属性
gPad->SetMargin(,,,)
四个参数分别表示左边距、右边距、下边距和上边距的大小
pad->SetBottomMargin(0.12);
pad-> SetTopMargin (0.04);
pad->SetRightMargin (0.02);
画板属性
自定义画板位置:
设置画布上的画板位置为倒品字形分布:
TCanvas *c1 = new TCanvas("c1","Histogram Drawing Options",200,10,700,900); TPad *pad1 = new TPad("pad1","The pad with the function",0.03,0.62,0.50,0.92); TPad *pad2 = new TPad("pad2","The pad with the histogram",0.51,0.62,0.98,0.92); TPad *pad3 = new TPad("pad3","The pad with the histogram",0.03,0.02,0.97,0.57); pad1->Draw(); pad2->Draw(); pad3->Draw();