Square TCanvas not square

I’m trying to save a square image from the contents of a TCanvas and do not manage. Is there something I’m missing?

This is very trivial to reproduce: simply creating a canvas with equal width and height and doing SaveAs will always return an image file (pdf, png, …) that is slightly less tall than it should to be square. Do the window menus count for the height? (imho they should not …)

Thanks a lot!!!

For completeness:
ROOT Version: 6.32.02
Platform: macosxarm64
Compiler: Apple clang version 15.0.0 (clang-1500.3.9.4)

Hi,

Could you perhaps specify whether the image is not square or the plot inside it? To better understand what you are after.

Best,
D

The actual size you see depends on the window decorations; when you create the canvas with a size, it is used for the window, and the actual canvas is reduced. One way to control the size is to use canvas->SetCanvasSize(); try this:

auto c = new TCanvas("c","c",300,300);
c->SetCanvasSize(500,500);
auto h = new TH1F("h","h",10,0,10);
h->Draw();
c->SaveAs("c500x500.png");

Since the canvas is 500x500 pixels, the window (which is smaller in this example!) now shows scroll bars (you can resize it, of course), but the saved image is 500x500. Note that by default the pad is automatically resized when you manually resize the window, but with the above the pad size remains fixed.
Interactive window:
image
Saved image:
c500x500

1 Like

Related:
https://root.cern/doc/master/classTCanvas.html#aefa471bc2211d7ddc8ac9b521d1aacd1

I just can confirm - to get reliable canvas size, one have to use SetCanvasSize(w,h) method.
Only this method guarantees pixel size both in interactive and batch mode.

1 Like

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