Can't change axis label for TH1 frame

void error()
{
TCanvas c(“c”, “c”, 800, 600);
// TH1 *h = c.DrawFrame(0.5, 0, 10.5, 10);
TH1F *h = new TH1F(“h”, “h”, 10, 0.5, 10.5);
h->GetYaxis()->SetRangeUser(0, 10);
TAxis *ax = h->GetXaxis();
ax->SetNdivisions(10, 0, 0, kTRUE);
for (int i=0; i<10; i++)
ax->SetBinLabel(i+1, Form("%d_%d", i, i+1));
h->SetStats(kFALSE);
h->Draw();
c.Modified();
c.SaveAs(“error.png”);
}

here is the code, if I draw with empty histogram, and then change its X-axis label, everything looks fine:

if I change h to TH1 frame, then I got something like this:

You can see that all labels curl up in the bottom left corner, I have no idea why so. Anybody can help? Thank you in advance.


Please read tips for efficient and successful posting and posting code

_ROOT Version: 6.16.00
_Platform: Ubuntu 20.04.2 LTS
_Compiler: gcc 9.3.0


https://root.cern/doc/master/TPad_8cxx_source.html#l01607
DrawFrame creates a histo with 1000 bins, so bins 1 to 10 overlap at the default zoom level, as you see in your example (zoom in and you’ll see all the labels are indeed in separate bins). You could do, for example:

  ax->SetBinLabel((i+1)*100, Form("%d_%d", i, i+1));

or similar.

Thank you. I encountered this problem many times, now I see why.

ChangeLabel is another the possibility.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.