Flip axes of TH2

Is it possible to flip the axes of a TH2 histogram? So x axis will be y and y will be x, and also the plot inside flips accordingly?

_ROOT Version: 6.26/04
Platform: osx
Compiler: PyROOT

For TH1 there is the “HBAR” option, but there is no such option for TH2; you will have to create and fill the “flipped” histogram yourself.

1 Like

Yes, you will need to make a flipped version of the histogram. You can also draw it as a surface seen from the top and rotate the x and y axis.

1 Like

thanks for the responses so far. I finally managed to make flip function, that works quite as expected:

def flip_th2_axes(h):
    hf = TH2F(f'{h.GetName()}_flip', f'{h.GetName()}_flip', h.GetNbinsY(), h.GetYaxis().GetXmin(), h.GetYaxis().GetXmax(), h.GetNbinsX(), h.GetXaxis().GetXmin(), h.GetXaxis().GetXmax())
    for i in range(h.GetNbinsX()):
        for j in range(h.GetNbinsY()):
            hf.SetBinContent(j, i, h.GetBinContent(i, j))
    return hf
1 Like