I have a canvas of 16 different histograms and for some reason their titles are being drawn on top of the pads. How can I move them up so that they don’t cover the plot?
Thanks
{
TH1F *h1= new TH1F("h1","histogram 1",100,-5,5);
h1->FillRandom("gaus",10000);
h1->Draw();
gPad->Update();
TPaveText* title = (TPaveText*)gPad->FindObject("title");
title->SetY1NDC(0.5);
title->SetY2NDC(0.6);
gPad->Modified();
gPad->Update();
}
1 Like
The only problem is I’m using pyroot, I tried
title = gPad.FindObject("title")
but this gives me the error:
title.SetY1NDC(0.5)
^^^^^^^^^^^^^^
AttributeError: 'TObject' object has no attribute 'SetY1NDC'
so I tried:
title = TPaveText(gPad.FindObject("title"))
but I got
TypeError: none of the 3 overloaded methods succeeded. Full details:
TPaveText::TPaveText(const TPaveText& pavetext) =>
TypeError: could not convert argument 1
TPaveText::TPaveText() =>
TypeError: takes at most 0 arguments (1 given)
TPaveText::TPaveText(double x1, double y1, double x2, double y2, const char* option = "br") =>
TypeError: takes at least 4 arguments (1 given)
An other way:
void titlepos(){
TH1F *h1= new TH1F("h1","histogram 1",100,-5,5);
h1->FillRandom("gaus",10000);
gStyle->SetTitleX(0.3);
gStyle->SetTitleY(0.5);
h1->Draw();
}
This works but as soon as the title is just above the plot it gets covered by the pad above
for i_plot in range(16):
canvas_x_channels.cd(i_plot+1)
x_temp_list = [sublist[i_plot] for sublist in analogues_x]
[x_histo.Draw("same") for x_histo in x_temp_list]
x_temp_list[0].SetTitle(f"x_channel{x_channels[i_plot]}")
gStyle.SetTitleX(0.3);
gStyle.SetTitleY(1.02)
It means your pads overlap… avoid that.
How do I avoid that? My pads are created like this:
my_canv = ROOT.TCanvas("my_canv ","my_canv ",1800,1400)
my_canv .Divide(4,4)
Well , I do not know enough of your case to know what should be change. I can try to reproduce it with he follwoing code. But it is fine for me:
{
auto c1 = new TCanvas("c1","multipads",1800,1400);
gStyle->SetOptStat(0);
c1->Divide(2,2);
auto h1 = new TH2F("h1","test1",10,0,1,20,0,20);
auto h2 = new TH2F("h2","test2",10,0,1,20,0,100);
auto h3 = new TH2F("h3","test3",10,0,1,20,-1,1);
auto h4 = new TH2F("h4","test4",10,0,1,20,0,1000);
c1->cd(1);
h1->Draw();
c1->cd(2);
h2->Draw();
c1->cd(3);
h3->Draw();
c1->cd(4);
h4->Draw();
}
Ok, thank you, it seems that my PadTopMargin was too small.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.