Plot disappears from canvas and does not display in notebook

Hello all,

I am using ipython and ipython notebook with rootnotes.py.
I have set the Xs, so I can see my plots in the Xs, and I am also saving them in png format.
Png saved files are fine, and also visible from ipython notebook running the commands:

from IPython.core.display import Image
Image(filename=‘test.png’)

However, the plots vanish from the Xs after few seconds and just the axis and the title axis remain (no data points, neither legends nor plot title).

I have tried this post aholzner.wordpress.com/tag/pyroot/ but it does not work.

I would like to have this fixed, basically because the plots that remain at the ipython notebook browser() are empty, I can not see them if I do not run lines above.

Thanks,
Ana.

Hi,

can you post the code you use? The common reason for canvases to come and go, is if the variable referencing them is going out of scope (e.g. when the code is executed from within a function body).

Cheers,
Wim

Hi,

the canvas is indeed within a function body, but I am returning that canvas (return canvas).

My code is a bit complex, but it also happens with simple examples as the one in root.cern.ch/drupal/content/ipyt … and-root-0 if you add it in the body of a function.

Is there any way to avoid the freing of that object?

Thanks,
Ana.

Ana,

[quote=“arodrig”]the canvas is indeed within a function body, but I am returning that canvas (return canvas).[/quote]but where is it subsequently going? I.e. what is holding a reference to it?

[quote]Is there any way to avoid the freing of that object?[/quote]Yes, using ROOT.SetOwnership(canvas, False). But make sure that someone deletes it at all, or the canvases will stay around until shutdown (at which point they’ll be cleaned up by the TROOT destructor).

Cheers,
Wim

Hello,

I’m calling the function from my ipython notebook (analyzer.makePlot(“massZ”)).

The ROOT.SetOwnership(canvas, False) does not seem to work.

I am using this makePlot function here:

github.com/ayrodrig/OutreachExe … er.py#L231

with very few modifications:

from OutreachExercise2010.DecaysToLeptons import rootnotes

#canvas = ROOT.TCanvas(histogram)
canvas = rootnotes.canvas(histogram)

#return plot
return canvas

Many thanks.

Best,
Ana.

Ana,

sorry, I can’t make sense of that: if the ownership is not on the python side, definitely python is not removing the canvas. Likewise, it is still not clear to me what would hold a return value from that function.

Maybe I’m misreading your initial post and it is not the canvas that disappears, but the data points? I.e. what is called “dataG” in the original code and to which the “plot” dictionary holds a reference whereas in your case it is removed as it is not returned after your change.

Cheers,
Wim

Wim,

indeed, you were not misreading the initial post, I just explained it wrong.
With your last post, you made me notice the difference.
Now I keep the whole plot dictionary in the object.

Thanks a lot for your help.

Best,
Ana.