def print_figure(fig, fmt='png', bbox_inches='tight', **kwargs):
 from matplotlib import rcParams
 if ((not fig.axes) and (not fig.lines)):
  return
 dpi = fig.dpi
 if (fmt == 'retina'):
  dpi = (dpi * 2)
  fmt = 'png'
 kw = {'format': fmt, 'facecolor': fig.get_facecolor(), 'edgecolor': fig.get_edgecolor(), 'dpi': dpi, 'bbox_inches': bbox_inches}
 kw.update(kwargs)
 bytes_io = BytesIO()
 fig.canvas.print_figure(bytes_io, **kw)
 data = bytes_io.getvalue()
 if (fmt == 'svg'):
  data = data.decode('utf-8')
 return data

def print_figure ( fig , fmt = 'png' , bbox_inches = 'tight' , ** kwargs ) : from matplotlib import rcParams if ( ( not fig . axes ) and ( not fig . lines ) ) : return dpi = fig . dpi if ( fmt == 'retina' ) : dpi = ( dpi * 2 ) fmt = 'png' kw = { 'format' : fmt , 'facecolor' : fig . get_facecolor ( ) , 'edgecolor' : fig . get_edgecolor ( ) , 'dpi' : dpi , 'bbox_inches' : bbox_inches } kw . update ( kwargs ) bytes_io = BytesIO ( ) fig . canvas . print_figure ( bytes_io , ** kw ) data = bytes_io . getvalue ( ) if ( fmt == 'svg' ) : data = data . decode ( 'utf-8' ) return data
print a figure to an image .