Colour palette on TH2 does not move with margin resizing


I am generating some figures in a loop, and I would like to move the right margin so that when I draw a TH2D object and add a title to the z-axis the title is actually visible. However, the colour palette does not move when I call gPad->SetRightMargin(), and it instead leaves a big gap between the side of the TH2D and the colour palette.

I have also tried calling gStyle as shown on this post, but the behaviour did not change. I am having the same problem as described in that post, actually, but can’t find a solution.

What can I do to automatically move a colour palette to stay flush with the histogram when I change the margin size?

Thank you.

ROOT Version: 6.23.01
Platform: Mac OS 10.14.6
Compiler: Not Provided

You need to set the margin before drawing anything (or first clear the pad, then set the margin, and then draw something).