# Flip axes of TH2

**URL:** <https://root-forum.cern.ch/t/flip-axes-of-th2/50820>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [July 15, 2022, 1:09pm UTC](https://root-forum.cern.ch/t/flip-axes-of-th2/50820 "2022-07-15T13:09:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xaratustra](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/xaratustra/32/13212_2.png) [@xaratustra](https://root-forum.cern.ch/u/xaratustra)\
**Post date:** [July 15, 2022, 1:09pm UTC](https://root-forum.cern.ch/t/flip-axes-of-th2/50820/1 "2022-07-15T13:09:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [July 15, 2022, 1:32pm UTC](https://root-forum.cern.ch/t/flip-axes-of-th2/50820/2 "2022-07-15T13:32:44Z")

</div>

For TH1 there is the “[HBAR](https://root.cern/doc/master/classTHistPainter.html#HP10)” option, but there is no such option for TH2; you will have to create and fill the “flipped” histogram yourself.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 20, 2022, 3:23pm UTC](https://root-forum.cern.ch/t/flip-axes-of-th2/50820/3 "2022-07-20T15:23:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xaratustra](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/xaratustra/32/13212_2.png) [@xaratustra](https://root-forum.cern.ch/u/xaratustra)\
**Post date:** [July 21, 2022, 1:50pm UTC](https://root-forum.cern.ch/t/flip-axes-of-th2/50820/4 "2022-07-21T13:50:57Z")

</div>

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

```auto
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

```
