SetPaintTextFormat

Hi,

I have a hopefully simple question. I use gStyle->SetPaintTextFormat(“4.1f”); to define the number of significant figures when plotting with the option “text”. I see that I can add some units by using:
gStyle->SetPaintTextFormat(“4.1f m”);
My question is, can I add text as a prefix to the bin content? Meaning, to plot the histograms entries as:
“myText BinContent"
instead of
"BinContent myText”

I tried:
gStyle->SetPaintTextFormat(“A 4.1f”);
and it does something odd…

Any advise is welcome :slight_smile:

-arely

The way this string is used in the painting method is:

   snprintf(format,32,"%s%s","%",gStyle->GetPaintTextFormat());

As you see a “%” is put in front of the string. So it is assumed that the string start with the format and can be followed by some text which can be seen as “units”… We can change the behavior but we take the risk to be backward incompatible.

If we allow text before the format then the % will be needed for the format ie:

gStyle->SetPaintTextFormat("Some text before %4.1f some text after");

The initial idea was to allow to have units…

Hi,

Thanks for the fast reply. This is for a one-time only histogram, so I don’t dare request any change in the format. I’ll try to find an alternative option. Just wanted to check it was not possible as it is…

Thanks!
Arely

As it is… no … code should be changed.
The only way would be that you loop over the bins and draw the texts yourself.
That’s not so complicated.

Hi @couet would you mind to explain this method a little more please, I want to do this for a TH2 is possible?

void textonhisto()
{
   auto h2 = new TH2F("h2","h2",2,0.,2,2.,0.,2);
   h2->Fill(0.5,0.5,1.);
   h2->Fill(0.5,1.5,2.);
   h2->Fill(1.5,0.5,3.);
   h2->Fill(1.5,1.5,4.);
   h2->Draw("col");
   for (int i=1; i<=2; i++) {
      for (int j=1; j<=2; j++) {
         auto t = new TText(h2->GetXaxis()->GetBinCenter(i),
                            h2->GetYaxis()->GetBinCenter(j),
                            Form("Content = %f kg",h2->GetBinContent(i,j)));
         t->SetTextAlign(22);
         t->Draw();
      }
   }
}

1 Like