Avoid empty TCanvas when the pyROOT program has ended

Dear all,
I wrote a python module that makes a TMultiGraph plot - you can see it in attachment.
The feature I am missing with respect to plain ROOT/C++ macro is the possibility to modify the graph and all the canvas elements after the module has ended.
If I run the module like this:

[quote]>>> import root_plot_multi_graph

root_plot_multi_graph.etreeminiparser( ‘C2V_no_mult_Perugia_fl1e15.xml’ )[/quote]
I get an empty canvas in the end - please see the attached image.
I have tried to add a gPad.Update() right before the end of the module but it didn’t help.
Many thanks in advance and best regards,
Marco Bomben
root_plot_multi_graph.py (2.76 KB)


Hi Marco,

the multigraph is being garbage collected.
A combination of the usage of DrawClone and keeping alive the canvas will help you.
See this minimal example:

# module test.py
import ROOT

def draw():
    c=ROOT.TCanvas()
    h=ROOT.TH1F('a','a',64,0,4)
    h.FillRandom('gaus')
    h.DrawClone()
    return c
>>> import test
>>> c= test.draw()
>>> # plot shown

Cheers,
Danilo

Thank you very much Danilo! By ‘DrawCloning’ both MultiGraph and Legend, and returning Canvas I got what I wanted.
For what concerns me you can consider this as solved.
Many thanks again,
Marco Bomben

Hi

I am facing the same issue, actually using 6.08 still results in empty canvas/pdf file - Any help appreciated


import ROOT

c=ROOT.TCanvas()
c.cd()
h=ROOT.TH1F('a','a',64,0,4)
h.FillRandom('gaus')
h.DrawClone()

c.Print("test.pdf")

If, in your python script, I use Draw instead of DrawClone then I get the correct plot.
I will investigate more.

I tried with some other example I have, and yes it seems that DrawClone in a python script does not produce the same output as Draw (DrawClone makes an empty plot). It must be something related to python because when I try the equivalent macro with C++, Draw and DrawClone both produce the same output. May be @etejedor has some ideas about it ?

Hello,
I have tried the code posted by Alkass and pdf is not empty using root version 6.11.02.
On top I can say that if I click on the canvas the canvas itself does not become empty.
Hope this helps,
Marco

PS I have an option to import root at python startup - I don’t know if that could make any difference

Hi @Alkass,
Did you run your Python script with python -i scriptname.py ? This will make sure the objects you create are still alive (the canvas among them). If the canvas is still blank when your script finishes, click on it with the mouse.
Cheers,
Enric

Same problem here, ROOT 6.10/02
I run it as python -i script.py
If I do histo.DrawClone() I initially see the curves, but as soon as the script ends they disappear. I’ll clone the objects first as a workaround…