Matplotlib: save plot to numpy array
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In Python and Matplotlib, it is easy to either display the plot as a popup window or save the plot as a PNG file. How can I instead save the plot to a numpy array in RGB format? 回答1: This is a handy trick for unit tests and the like, when you need to do a pixel-to-pixel comparison with a saved plot. One way is to use fig.canvas.tostring_rgb and then numpy.fromstring with the approriate dtype. There are other ways as well, but this is the one I tend to use. E.g. import matplotlib.pyplot as plt import numpy as np # Make a random plot... fig =