How to rotate bin labels on THF2?

I have a 2D histogram with bin labels. However, the labels overlap into adjacent bins. I want to rotate the labels so this doesn’t happen.

I see the option to use TEXT45 like hist.Draw(“TEXT45”), but TEXT90 doesn’t seem to work… Any tips? Is 45 the only angle by which we can rotate these?

Just to clarify I’m trying to rotate the BIN labels (numbers inside each bin), not the axis labels.

(I could provide an example histogram if that’s helpful but the problem isn’t specific to my histogram)


ROOT Version: 6.26/06
Platform: Linux x86 64bit
Compiler: gcc


Welcome to the ROOT forum.

Can you provide a small example showing the problem ?
In principle they should not overlap.

Yep!

This one looks fine with TEXT45 (no rotation works for this too)

image

But in another histogram with the bins closer together, there’s less space for the text, so I want to do a 90 degree rotation (45 is fine I suppose but with no rotation the text is completely unreadable).

image

What do you mean by “in principle they should not overlap” – is the text supposed to be auto-fit to the bin size?

That’s not the labels, that’s the bins’ content.
I understand now.
To rotate at 90 degrees, TEXT90 should work. Have you tried ?

Content, got it (the text things are labels of content, but if that’s the term we use here, sure).

As mentioned in the initial post, TEXT90 doesn’t seem to work, but I should’ve provided more details – here’s what I see when I try TEXT90:

image

TEXT180:

image

TEXT:

image

When you say “should” work, what do you mean?

The draw_hist function here is what I use in the main code, so I kept some irrelevant options in case it helps to see those.

from ROOT import TFile
from ROOT import TCanvas
from ROOT import gStyle

def draw_hist(out_dir, title_prefix, hist, name):
    """Make a histogram."""
    canvas = TCanvas()
    canvas.SetTopMargin(0.1)
    canvas.SetBottomMargin(0.15)
    canvas.SetLeftMargin(0.15)
    canvas.SetRightMargin(0.15)
    gStyle.SetOptTitle(1)
    gStyle.SetOptStat(0)
    gStyle.SetPaintTextFormat(".3f")
    title = title_prefix + name
    hist.SetDirectory(0)
    hist.SetTitle(title)
    hist.SetTitleSize(0.015, "t")
    hist.SetMaximum(1.0)
    hist.SetMinimum(0.0)
    hist.Draw("COLZ TEXT90")
    canvas.SaveAs(out_dir + title_prefix + name + ".png")
    canvas.Close()

f = TFile.Open("histogram.root","r")
hist = f.Get("Hist")

draw_hist(".", "blabla", hist, "blabla")

histogtam.root is attached :slight_smile:

histogram.root (4.4 KB)

Expected output: text with 90 degree rotated text

Actual output:
image

@couet I can confirm this problem.
This works:
root histogram.root -e 'Hist->Draw("COLZ TEXT89");'
root histogram.root -e 'Hist->Draw("COLZ TEXT92");'
This breaks:
root histogram.root -e 'Hist->Draw("COLZ TEXT90");'
root histogram.root -e 'Hist->Draw("COLZ TEXT91");'

It works for me:

{
   auto c01 = new TCanvas("c01","c01",800,800);
   auto htext2 = new TH2F("htext2","Option TEXT on 2D histograms ",10,-4,4,10,-20,20);
   float px, py;
   for (Int_t i = 0; i < 25000; i++) {
      gRandom->Rannor(px,py);
      htext2->Fill(px,5*py);
   }
   htext2->SetMarkerSize(1.8);
   htext2->Draw("TEXT90");
}

htext2->Draw("COLZ TEXT90"); // 89 and 92 are fine, 90 and 91 are broken

Combination of options is not always foreseen. The way to do it is:

htext2->Draw("COLZ");
htext2->Draw("TEXT90 SAME");

So, it’s a new bug in ROOT 6 (it’s fine in ROOT 5). It looks like any “0” and/or “1” in the “TEXTnn” drawing option get confused with “COL0”/“COLZ0” and/or “COL1”/“COLZ1”.

Fix here:

Thanks to have seen it.

Will it be possible to make the documentation reflect the fact that TEXT90 is possible? Now, in ROOT: THistPainter Class Reference, it says that nn in TEXTnn should be 0 < nn < 90, so 90 is not included, while it should be.

1 Like

Done. Thanks

Thanks everyone!

2 Likes

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