Making the both axes square on the pad


Is there a way to force TH2 to draw with both axes the same length? Or more importantly same scale? I’m trying to draw a TH2F where the axes are both in the same units and I’d like it to print that way. Trouble is, I also adjust the pad margins and this seems to affect the overall length of one axis relative to the other.


Yes, of course, if you change the margins the plot may not be square any more. Can you send a small example showing what you thingk is wrong ?


I’m attaching a script that just draws the pad with the same code that I use. I’ve removed the part that fills the histogram. The reason that I need to adjust the pad margins is because otherwise I lose the labels on the color axis. Is there a way to force the histogram to draw each axis to the same scale regardless of what is done to the pad?

test.C (1.8 KB)

You can use gPad->SetFixedAspectRatio();

Hi Again,

So gPad is the global pointer to the current active pad. I have mulitple canvases open, so I figured that since TCanvas inherits from TPad I could use the TPad::SetFixedAspectRatio() on the canvas itself. For example:
TCanvas *c = new TCanvas(“c”,"",500,500);
However, using gStyle->SetPadXXXMargin( ) still causes the aspect ratio to change between the x and y axes.

I’m also using root version 4.00/08 and do not have permission to upgrade (at least for the time being).


yes sorry you should do it on canvas


I do it on the canvas, like I said above, and it still doesn’t work. I notice no difference between when I use it and when I don’t. Do I have to somehow grab the pad that’s in the canvas after the TH2F is drawn and set it then? If so, what’s the easiest way to do it?


With the following values in your macro you get a square:

  Float_t leftmargin  = 0.1;
  Float_t rightmargin = 0.12;
  Float_t topmargin   = 0.045;


I dont want to have edit the margins to both get the color axis and to get the x-y axes square.

When I dump the TCanvas object I find that these are the values I would like to change in my script. The methods TCanvas::GetX1(), etc., return me these values.

fX1 -1.40755 X of lower X coordinate
fY1 -1.59144 Y of lower Y coordinate
fX2 1.36981 X of upper X coordinate
fY2 1.1323 Y of upper Y coordinate

To change these values I’ve found that TPad::Range(x1,y1,x2,y2) works; however, I can’t figureout how to make the histogram adjust to these new settings. TPad::RangeChanged(), Update(), and Draw() seem to do nothing.


see the short example below

//method1. compute ymax to get a commensurate range
// 1 pixel must correspond to the same range in x and y
TCanvas c1(“c1”,“c1”,10,10,800,600);
Float_t xmin = 0;
Float_t xmax = 20;
Float_t ymin = -2;
Int_t npx = gPad->GetWw();
Int_t npy = gPad->GetWh();
Float_t ymax = ymin + (xmax-xmin)*npy/npx;
TH2F h(“h”,"",20,xmin,xmax,20,ymin,ymax);
TArc a1(6,4,6);

//method2. set a square virtual canvas size in a non-square canvas
TCanvas c2(“c2”,“c2”,200,50,800,600);
TArc a2(10,10,10);



Method 2 works if I dont adjust the pad margins. See attached for an extreme example. Trouble is, I would like to adjust the pad margins, and have square axes without having to guess at their proportions everytime.

test.C (2.11 KB)