#!/usr/bin/env python # coding: utf-8 # In[5]: class Power(object): def __init__(self, power): self.power = power # In[24]: get_ipython().run_line_magic('matplotlib', 'inline') import numpy as np from IPython.core.pylabtools import print_figure import matplotlib.pyplot as plt def power_png(p): fig, ax = plt.subplots() x = np.linspace(-5,5) y = x ** p.power ax.plot(x,y) buf = print_figure(fig, 'png') plt.close(fig) # close the figure return buf # In[25]: p = Power(5) # In[27]: df = get_ipython().display_formatter png = df.formatters['image/png'] png.for_type(Power, power_png) # In[28]: p