PyRoot TCanvas is not persistent

ROOT.TCanvas is not persistent and hence there are no change to manipulate manually.
If I am not correct please let me know how to do that, otherwise is there any development to provide that.
Thanks
Soumen

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi,
What do you mean when you say ROOT.TCanvas is not persistent?

python garbage collection

I want to mean when I write a ROOT macro like
c=ROOT.TCanvas(“c”,“c”)

A window of that canvas just appear and then it disappears.
But if I write similar code in c++

TCanvas c;
Then a canvas really appears, and we can modify stuff, like axis labelling drawn on the canvas.

By the way I am using root version 6.10.04

Is your canvas “c” created in a function that returns? If it’s a global variable it should stick around, but if it’s in a function, you need to return the “c” (along with any TGraphs, TArrows, TLegends etc) otherwise the python garbage collection deletes them. E.g.:

def make_graph():
    c = ROOT.TCanvas()
    h = ROOT.TH1D("h","h",10,0,10)
    h.Draw()
    return c,h

// Global scope
c,h = make_graph()

import ROOT
def make_graph():
c = ROOT.TCanvas()
h = ROOT.TH1D(“h”,“h”,10,0,10)
h.Draw()
return c,h
if name==“main”:
c,h = make_graph()
c.SaveAs(‘cvs.png’)

I agree, say for example this piece of code will save my canvas, but if write simmilar code in c++ the canvas is drawn on a TBrowser, and we can edit (change axis label or anything else) the Canvas on the TBrowser and then we can save that after some manipulation. But this python code does not give me that facility.

Sorry, I probably should have searched it before. here is the exactly same issue.

I had to use python -i filename.py

Yes, exactly :slight_smile: The -i flag keeps the Python interpreter alive, and therefore your TCanvas too.

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