TCanvas disappears with pyroot

Hi everyone,

I have an issue with TCanvas and pyroot. When I run in the terminal the very simple macro “” with the command “python

import ROOT canv = ROOT.TCanvas()

The canvas appears and half a second after it disappears. I know by doing python -i the canvas stays but then I have to write the command “.q” to exit python, which is painful when I have to do it a lot of time…

I am definitively not a pyroot expert and I searched on internet solutions such as SetOwnership but I didn’t understand what to do precisely.

Please, can anyone explain me what I have to do ?

Thanks a lot for your help,


when the program terminates, all of the memory associated to it is given back to the os, including the allocated objects, like the TCanvas: this is independent of pyroot.
If you want, you can save your canvases as images using the method TCanvas::Print.



Thanks for you answer.

Is there a way to keep the canvas on the screen even after the program terminates ?

I would like to close the canvas with the red cross or by doing cmd + Q (I’m on mac) instead of writing the command “.q”.

Otherwise, I guess the best solution would be the raw_input(“Press enter to continue…”)


Hi Misa,

No and I think this is a sane feature of all operating systems.

This is a good option. It allows to write a program in python that does 99% of the work and allows you to refine the final few plots for the forthcoming presentation/publication.



if all you need is the GUI (i.e. no CLI at all), you can use:ROOT.gApplication.Run()instead of raw_input(). The application will then close if you choose “Quit ROOT” from the canvas (but not if you only close the canvas itself).


Hi Wim,

Thanks a lot, that was exactly what I was looking for ! It seems definitively the best option to me.
However, what do you mean by “CLI” ?



CLI = command line interface, i.e. the python prompt.


In rootpy [1] we implemented a hook [2] that prevents a program from continuing (or exiting) until all current canvasses are closed:

from rootpy.interactive import wait
from rootpy.plotting import Canvas, Pad

for color in ('red', 'green', 'blue'):
    canvas = Canvas()
    pad = Pad(0, 0, 1, 1, color=color)

canvas1 = Canvas()
canvas2 = Canvas()
canvas3 = Canvas()

You can insert a wait() at any stage you want to pause and inspect open canvasses. When those canvasses are closed, the program continues.

[2] …

1 Like

Thanks, Noel!

I have errors with 6.22 and 6.24. Possibly, rootpy is not maintained - ?