import matplotlib.pyplot as plt
plt.ion()
fig = plt.gcf()
plt.plot([1,3,2])
[<matplotlib.lines.Line2D at 0x115a1c668>]
class MyFigure:
width = None
height = None
obj = None
def _repr_png_(self):
metadata = {}
if self.width:
metadata['width'] = self.width
if self.height:
metadata['height'] = self.height
pngdata = self.render_png()
return pngdata, metadata
def render_png(self):
if isinstance(self.obj, int):
pass #...
else:
data, metadata = get_ipython().display_formatter.format(self.obj)
if 'image/png' in data:
return data['image/png']
img = MyFigure()
img
/Users/minrk/dev/ip/ipython/IPython/core/formatters.py:345: FormatterWarning: image/png formatter returned invalid type <class 'tuple'> (expected (<class 'bytes'>, <class 'str'>)) for object: <__main__.MyFigure object at 0x117f99240> FormatterWarning
<__main__.MyFigure at 0x117f99240>
img.obj = fig
img.width = 20
img.width = 100
img