def mplfig_to_npimage(fig):
 from matplotlib.backends.backend_agg import FigureCanvasAgg
 canvas = FigureCanvasAgg(fig)
 canvas.draw()
 (l, b, w, h) = canvas.figure.bbox.bounds
 (w, h) = (int(w), int(h))
 buf = canvas.tostring_rgb()
 image = np.fromstring(buf, dtype=np.uint8)
 return image.reshape(h, w, 3)

def mplfig_to_npimage ( fig ) : from matplotlib . backends . backend_agg import FigureCanvasAgg canvas = FigureCanvasAgg ( fig ) canvas . draw ( ) ( l , b , w , h ) = canvas . figure . bbox . bounds ( w , h ) = ( int ( w ) , int ( h ) ) buf = canvas . tostring_rgb ( ) image = np . fromstring ( buf , dtype = np . uint8 ) return image . reshape ( h , w , 3 )
converts a matplotlib figure to a rgb frame after updating the canvas .