Problems with bin labels on TH2Poly

Hello,

I need to make a histogram with empty spaces between some bins, but continuous 2D bin numbering (ignoring the spaces). Probably TH2Poly with alphanumeric bin names is the best option (although I’ll have to subclass the TH2Poly so Fill(int1, int2, val) with call Fill(string(int1), string(int2), val)). I started simple and already have a problem. My bin labels do not seem to go where they should, below the bins. I would be grateful for a hint on what I am doing wrong:

import ROOT

c = ROOT.TCanvas()

x1 = [0, 2, 0, 2]
y1 = [0, 0, 2, 2]
x2 = [1, 3, 1, 3]
y2 = [1, 1, 3, 3]

h = ROOT.TH2Poly()

for i in range(len(x1)):
	h.AddBin(x1[i], y1[i], x2[i], y2[i])

h.GetXaxis().SetBinLabel(1, "0")
h.GetXaxis().SetBinLabel(2, "1")
h.GetYaxis().SetBinLabel(1, "0")
h.GetYaxis().SetBinLabel(2, "1")

h.GetXaxis().CenterLabels()
h.GetYaxis().CenterLabels()

h.Fill(0.5, 0.5)
	
h.Draw()

I attach a screenshot of the result.
th2poly_binlabels


ROOT Version: 6.26.02


SetBinLabel does not make sense for TH2Poly.
I’ll have a closer look, to find out how to make such custom labels for TH2Poly.

Thanks. However, it seem then that I either have not understand the API, or the API of TH2Poly is somewhat misleading, as it shows Fill(char , char, double):

https://root.cern.ch/doc/master/classTH2Poly.html#ae5650ff1549876c01110a67f5a22e4df

Yes, one can access the poly-bins by name… but that does not reflect on the axis. As in this example.

OK, So I understand that when filling with a class that has IsInside() and inherits from TNamed, the poly-bin name becomes the name of the object with thick the TH2Poly was filled. But that, and the example with Europe refers to one string in Fill(). How to give a bin a name with 2 strings, as in the Fill() I referred to?

Indeed this FIll method is not used in TH2Poly. It is overwritten because TH2Poly inherits for TH2.
The Doxygen documentation should make it more clear.
See:

https://root.cern.ch/doc/master/TH2Poly_8h_source.html#l00133

This PR makes it more clear:

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