SetPaintTextFormat

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