Last active
December 17, 2020 16:12
-
-
Save mazurov/6194738 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Helper module for displaying ROOT canvases in ipython notebooks | |
Usage example: | |
# Save this file as rootnotes.py to your working directory. | |
import rootnotes | |
c1 = rootnotes.default_canvas() | |
fun1 = TF1( 'fun1', 'abs(sin(x)/x)', 0, 10) | |
c1.SetGridx() | |
c1.SetGridy() | |
fun1.Draw() | |
c1 | |
More examples: http://mazurov.github.io/webfest2013/ | |
@author [email protected] | |
@author [email protected] | |
@date 2013-08-09 | |
""" | |
import ROOT | |
ROOT.gROOT.SetBatch() | |
import tempfile | |
from IPython.core import display | |
def canvas(name="icanvas", size=(800, 600)): | |
"""Helper method for creating canvas""" | |
# Check if icanvas already exists | |
canvas = ROOT.gROOT.FindObject(name) | |
assert len(size) == 2 | |
if canvas: | |
return canvas | |
else: | |
return ROOT.TCanvas(name, name, size[0], size[1]) | |
def default_canvas(name="icanvas", size=(800, 600)): | |
""" depricated """ | |
return canvas(name=name, size=size) | |
def _display_canvas(canvas): | |
file = tempfile.NamedTemporaryFile(suffix=".png") | |
canvas.SaveAs(file.name) | |
ip_img = display.Image(filename=file.name, format='png', embed=True) | |
return ip_img._repr_png_() | |
def _display_any(obj): | |
file = tempfile.NamedTemporaryFile(suffix=".png") | |
obj.Draw() | |
ROOT.gPad.SaveAs(file.name) | |
ip_img = display.Image(filename=file.name, format='png', embed=True) | |
return ip_img._repr_png_() | |
# register display function with PNG formatter: | |
png_formatter = get_ipython().display_formatter.formatters['image/png'] # noqa | |
# Register ROOT types in ipython | |
# | |
# In [1]: canvas = rootnotes.canvas() | |
# In [2]: canvas | |
# Out [2]: [image will be here] | |
png_formatter.for_type(ROOT.TCanvas, _display_canvas) | |
png_formatter.for_type(ROOT.TF1, _display_any) |
Well done Mazurov! I just got this to work following:
http://thomassileo.com/blog/2012/11/19/setup-a-remote-ipython-notebook-server-with-numpyscipymaltplotlibpandas-in-a-virtualenv-on-ubuntu-server/
hi
I have a problem with " import rootnotes"
the err is
No module named 'rootnotes'
File "/Users/vahid/Documents/pycodes/invariantmass.py", line 2, in
import rootnotes
plz help
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, a really useful gist. One remark:
On Windows the call
tempfile.NamedTemporaryFile(suffix=".png")
must be changed totempfile.NamedTemporaryFile(suffix=".png",delete=False)
, otherwise inline plots would not work in the IPython notebook on this platform.