#!/usr/bin/env python # coding: utf-8 # In[34]: import matplotlib.pyplot as plt plt.ion() fig = plt.gcf() plt.plot([1,3,2]) # In[52]: 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'] # In[53]: img = MyFigure() img # In[55]: img.obj = fig img.width = 20 # In[58]: img.width = 100 img