hist->GetXaxis()->SetLabelOffset(100); // 範囲外へ元の数字のラベルを飛ばしてしまう hist->GetXaxis()->SetNdivisions(3,1,1); TLatex *label = new TLatex(); label->SetTextSize(0.07); label->DrawLatexNDC(0.18,0.04,"K_{L}#rightarrow3#pi^{0}");// TLatexでtextを書いていく label->DrawLatexNDC(0.45,0.04,"K_{L}#rightarrow2#pi^{0}"); label->DrawLatexNDC(0.72,0.04,"K_{L}#rightarrow2#gamma");
hist->GetXaxis()->SetBinLabel(1,"K_{L}#rightarrow3#pi^{0}");//第一引数はibin hist->GetXaxis()->SetBinLabel(2,"K_{L}#rightarrow2#pi^{0}");// ibin=0 はunderflow hist->GetXaxis()->SetBinLabel(3,"K_{L}#rightarrow2#gamma"); // ibin=nbin+1 はoverflow を表す hist->GetXaxis()->SetLabelSize(0.07);
フレームを書くまでのサンプルコード
const Int_t NMode = 3; const TString LabelText[NMode] = { "K_{L}#rightarrow3#pi^{0}", "K_{L}#rightarrow2#pi^{0}", "K_{L}#rightarrow2#gamma" }; TH1D *frame = new TH1D("frame","",NMode,0,(Double_t)NMode); frame->SetBit(TH1::kNoStats); ///// X-axis ///// frame->GetXaxis()->SetNdivisions(NMode,1,1,false); frame->GetXaxis()->SetLabelSize(0.08); for(Int_t i = 0 ; i < NMode ; ++i) frame->GetXaxis()->SetBinLabel(i+1,LabelText[i]); ///// Y-axis ///// const Double_t Ymin = 0.5; const Double_t Ymax = 1.5; frame->SetMinimum(Ymin); frame->SetMaximum(Ymax); frame->SetYTitle("Y Title"); frame->SetTitleOffset(0.8,"Y"); frame->SetTickLength(0.01,"Y"); TCanvas *c = new TCanvas("c","c",800,600); c->SetTopMargin(0.1); c->SetBottomMargin(0.1); c->SetLeftMargin(0.1); c->SetRightMargin(0.05); frame->Draw();
DrawFrame functionで生成されるhistogramは、実はnbin=1000をもつ。
必要なbin数にまでrebinしてまとめてやった上でSetBinLabelを呼べば良い。
ただし、bin数が1000を割り切らない場合は、ラベルの位置が少々ずれてしまうのが難点。
上のサンプルコードのようにやる方がよいかな。
const Int_t NLabel = 3; const TString LabelText[NLabel] = { "K_{L}#rightarrow3#pi^{0}", "K_{L}#rightarrow2#pi^{0}", "K_{L}#rightarrow2#gamma" }; TCanvas *c = new TCanvas("c","c",700,500); TH1F *frame = (TH1F*)c->DrawFrame(0,3,3,4.5); frame->GetXaxis()->SetNdivisions(NLabel,1,1,false); frame->Rebin( frame->GetNbinsX()/NLabel ); for(Int_t i = 0 ; i < NLabel ; ++i) frame->GetXaxis()->SetBinLabel(i+1,LabelText[i]); frame->GetXaxis()->SetLabelSize(0.07);