import matplotlib.pyplot as pltimport numpy as npimport iox=np.arange(10)y=x#plt.plot(x,y)#canvas = plt.get_current_fig_manager().canvas#canvas.draw()fig=plt.figure()plt.plot(x,y)canvas=fig.canvas#上面这段代码和上面注释掉的代码效果一样#方法1buffer = io.BytesIO()canvas.print_png(buffer)data=buffer.getvalue()buffer.close()#方法2buf, size = canvas.print_to_buffer()image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)buffer=io.BytesIO()image.save(buffer,'PNG')data=buffer.getvalue()buffer.close()with open('hhh.png',mode='wb') as f:f.write(data)#f=open('hh.png',mode='wb')#f.write(data)#f.close()
以上这篇matplotlib.pyplot画图 图片的二进制流的获取方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持VEVB武林网。