# TCanvas::Print PNG outputs fraction of figure when canvas size is declared

**URL:** <https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011>\
**Category:** ROOT\
**Tags:** graphics\
**Created:** [August 24, 2017, 8:13pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011 "2017-08-24T20:13:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [August 24, 2017, 8:13pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/1 "2017-08-24T20:13:21Z")

</div>

When declaring the size of a canvas in the constructor the `Print` method to `png` outputs only the upper right corner of the figure. Without specification of the dimension the `Print` method works as expected. (I assume I will have to wait until @couet returns for an answer.)

 ![c1](https://root-forum.cern.ch/uploads/default/original/2X/f/f96abf9d7933570f8faded88ac9e190a4b243d29.png)  
 ![c2](https://root-forum.cern.ch/uploads/default/original/2X/9/901a98a9c6283fe5ed8291d85e78c4ec73acbd23.png)  
 ![c3](https://root-forum.cern.ch/images/transparent.png)  
(The third figure is not uploading correctly here for some reason.)

```auto
{
   TH1F *h = new TH1F("h","",100, -2, 2);
   h->FillRandom("gaus",5000);

   TCanvas *c1 = new TCanvas("c1");
   c1->cd();
   h->SetTitle("No Canvas Position or Size Info");
   h->Draw();
   c1->Print("c1.png");

   TCanvas *c2 = new TCanvas("c2", "", 800, 600);
   c2->cd();
   h->SetTitle("Canvas with Size Information");
   h->Draw();
   c2->Print("c2.png");

   TCanvas *c3 = new TCanvas("c3", "", 0,0, 800, 600);
   c3->cd();
   h->SetTitle("Canvas with Position and Size Information");
   h->Draw();
   c3->Print("c3.png");
}

```

Generated on ROOT 6.10.04 MacOS X 10.12.6

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 24, 2017, 8:55pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/2 "2017-08-24T20:55:02Z")

</div>

> [@SetBatch causes different graphics output for pngs](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/8):
>
> I’m afraid [TCanvas::SetWindowSize](https://root.cern.ch/root/html534/TCanvas.html#TCanvas:SetWindowSize) is completely unreliable (it can misbehave even in interactive mode). When I try (ROOT 5.34 and 6.04.02): { Int\_t w = 800; Int\_t h = 600; TCanvas \*c; gROOT-\>SetBatch(kFALSE); c = new TCanvas("c", "c", w, h); c-\>Modified(); c-\>Update(); std::cout \<\< c-\>GetWw() \<\< " " \<\< c-\>GetWh() \<\< std::endl; c-\>SetWindowSize(w + (w - c-\>GetWw()), h + (h - c-\>GetWh())); c-\>Modified(); c-\>Update(); std::cout \<\< c-\>GetWw() \<\< " " \<\< c-\>GetWh() \<\< std::endl; …

> [@Print / saveas png draws axis title twice with offset](https://root-forum.cern.ch/t/print-saveas-png-draws-axis-title-twice-with-offset/19142/7):
>
> I am able to reproduce this problem on my Ubuntu 14.04. It is related to two lines in the “rootlogon.C”: gStyle-\>SetCanvasDefH(RequestedH); gStyle-\>SetCanvasDefW(RequestedW); As soon as “RequestedH” and/or “RequestedW” exceed physical dimensions of my screen, I also get “spurious” contents in the “bottom” and/or “right” side in the “test1.png” file (but not in another saved pictures and not on the screen). I can clearly see that, when the “png\_double\_x\_axis\_title” macro starts, ROOT opens a…

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [August 24, 2017, 8:57pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/3 "2017-08-24T20:57:29Z")

</div>

I’m not sure that exactly covers what is going on here. I know for sure that my screen is larger than 800x600 pixels.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [August 24, 2017, 9:08pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/4 "2017-08-24T21:08:04Z")

</div>

This is definitely the same issue as:

> [@Saved canvas is magnified in top left corner](https://root-forum.cern.ch/t/saved-canvas-is-magnified-in-top-left-corner/19444/3):
>
> Ok, thanks So, temporarily, what is working for me is to just execute my script with the “-b” flag as well ie root -l script.C -b so in the silent mode actually my canvases are save in the dimensions and are filled properly if I open them afterwards. Regards

Also, the output is the same dimension in all three cases roughly 800x600. It seems that the `c2` canvas is exploded.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 25, 2017, 8:37am UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/5 "2017-08-25T08:37:13Z")

</div>

I think all these problems are related to some misbehaviour during interaction between ROOT and the window manager.

There are two JIRA reports that I know of:  
[https://sft.its.cern.ch/jira/browse/ROOT-7469](https://sft.its.cern.ch/jira/browse/ROOT-7469)  
[https://sft.its.cern.ch/jira/browse/ROOT-7552](https://sft.its.cern.ch/jira/browse/ROOT-7552)

BTW. I have just checked them and I can confirm that both reported problems (the completely unreliable TCanvas::SetWindowSize and the one related to “RequestedH” and/or “RequestedW”) are still there in the newest ROOT v5-34-00-patches and in 6.10.04.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [August 28, 2017, 10:34pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/6 "2017-08-28T22:34:06Z")

</div>

To make this even worse I discovered today that the outcome of the png depends on which monitor my terminal is sitting it. I can run the script with the terminal in a small monitor and get a corner and then move the terminal to a large monitor and get the entire image.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [September 5, 2017, 2:31pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/7 "2017-09-05T14:31:48Z")

</div>

Just a bump so this doesn’t get locked before it is resolved.

---

<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:** [September 6, 2017, 7:59am UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/8 "2017-09-06T07:59:42Z")

</div>

I just ran your macro on Mac with ROOT version 6.11/01 . I ran it in interactive mode and in batch mode. The 3 png files it produces are correct in both cases. I do not see the effect you are mentioning in your first post.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [September 6, 2017, 6:50pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/9 "2017-09-06T18:50:45Z")

</div>

I believe this is dependent on the monitor configuration. I have a somewhat unusual setup on this machine with four monitor all over various dimensions. I have the following (I don’t know how to have it list the relative positions, but I’d be willing to experiment if needed.):

| Monitor | Dimension | Rotation |
| 1 | 3360 x 1890 | 0 |
| 2 | 1200 x 1920 | 0 |
| 3 | 1920 x 1080 | 270 |
| 4 | 1200 x 1600 | 90 |

The total resolution reported from `xrandr` is is 7680 x 1898

In addition, the clipping only occurs when the terminal is placed in either of the rotated monitors. The problem does not show up in batch mode (I have noticed that images made in batch mode are sharper than those created outside.)

Is there something else I can provide to help your investigation?

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [September 6, 2017, 7:12pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/10 "2017-09-06T19:12:03Z")

</div>

After some further testing, I seem to have narrowed it down to the fact that there are three rotations. If I reduce the number of rotations to one (All 0 degrees) or two (0 deg and 90 deg OR 0 deg and 270 deg) then the issue seems to go away. I didn’t realize initially that this would be such a strange corner case.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 6, 2017, 8:12pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/11 "2017-09-06T20:12:28Z")

</div>

I don’t use any fancy rotations and I clearly get today both problems that were reported more than 2 years ago.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [September 6, 2017, 8:55pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/12 "2017-09-06T20:55:40Z")

</div>

@Wile_E_Coyote would you be willing to provide your display setup?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 6, 2017, 9:01pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/13 "2017-09-06T21:01:10Z")

</div>

This is an old HP EliteBook 8530p laptop running Ubuntu 14.04.5 LTS / x86\_64:

```auto
[...] $ xrandr
Screen 0: minimum 320 x 200, current 1680 x 1050, maximum 8192 x 8192
VGA-0 disconnected (normal left inverted right x axis y axis)
LVDS connected primary 1680x1050+0+0 (normal left inverted right x axis y axis) 331mm x 207mm
   1680x1050 60.0*+
   1400x1050 60.0
(...)
   640x480 59.4
DVI-0 disconnected (normal left inverted right x axis y axis)
HDMI-0 disconnected (normal left inverted right x axis y axis)

```

---

<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:** [September 8, 2017, 6:49am UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/14 "2017-09-08T06:49:07Z")

</div>

I ran again the macro using the X11 backend on Mac both in natch and interactive mode and the 3 png files are correct. It was hard to debug a problem I cannot reproduce …

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [September 8, 2017, 3:32pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/15 "2017-09-08T15:32:25Z")

</div>

I’ve further narrowed this issue down. It doesn’t have anything to do with the construction of the Canvas. It just so happened that when specifying the size alone the canvas was created on the largest monitor in my setup (3360 x 1890), while in the other two case the canvases were created on other windows. If I move any canvas to the large monitor and select print I only get a corner of the image. Further more this screen has the resolution scaled, if I remove the scaling and use the native resolution (3840 x 1920) then the issue disappears.

---

<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:** [September 11, 2017, 8:48am UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/16 "2017-09-11T08:48:57Z")

</div>

I see. I am sorry but i do not have such setup to test/debug that.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [September 12, 2017, 10:09pm UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/17 "2017-09-12T22:09:43Z")

</div>

You do not have access to a Mac? Wile, indicated that the problem is also visible on Ubuntu. I believe all that needs to be done is to scale the resolution through the Display Settings. Is there something I can do on my end to provide more debugging information? I probably just need a couple of ideas of where to look to get started.

---

<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:** [September 13, 2017, 7:28am UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/18 "2017-09-13T07:28:40Z")

</div>

> [@ksmith](#):
>
> You do not have access to a Mac?

Yes that’s my machine. But the macro you provided are giving me the right png output whatever I try. May be I missed something.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 13, 2017, 7:52am UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/19 "2017-09-13T07:52:26Z")

</div>

I think the problem originates somewhere in [TCanvas::SetWindowSize which sometimes fails in interactive mode](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/8), but here’s your own, more than 2 years old, post concerning the second problem:

> [@Print / saveas png draws axis title twice with offset](https://root-forum.cern.ch/t/print-saveas-png-draws-axis-title-twice-with-offset/19142/8):
>
> I can reproduce the effect on Mac with the X11 version using: void png\_double\_x\_axis\_title(){ gStyle-\>SetCanvasDefH(2000); gStyle-\>SetCanvasDefW(1000); TF1 \*fa1 = new TF1("fa1","sin(x)/x",0,10); TCanvas \*c1 = new TCanvas(); fa1-\>Draw(); fa1-\>GetXaxis()-\>SetTitle("X-axis"); fa1-\>GetYaxis()-\>SetTitle("Y-axis"); c1-\>Modified(); c1-\>Update(); c1-\>Print("test1.png"); } It gives me the attached plot.

---

<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:** [September 13, 2017, 8:56am UTC](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011/20 "2017-09-13T08:56:31Z")

</div>

Thanks for the reminder. I am looking at it again now. There is an even simpler reproducer.

```auto
void png_double_x_axis_title(){
   TCanvas *c1 = new TCanvas("c1","c1",0,0,1000,2000);
   c1->DrawFrame(0.,0.,10.,10.);
   c1->Print("test1.gif");
   c1->Print("test1.png");
   c1->Print("test1.pdf");
}

```

So it shows only with the X11 version in interactive mode. It works in batch mode. gif and pdf work in both mode. The Cocoa version is fine in all cases.

[Next page](https://root-forum.cern.ch/t/tcanvas-print-png-outputs-fraction-of-figure-when-canvas-size-is-declared/26011.md?page=2)
