我怎样通过脚本获取Matplotlib绘图的图片?
我怎样通过脚本获取Matplotlib绘图的图片?
这个问题已经有答案了:
这是我的代码:
hours = [hour1, hour2, hour3, hour4, hour5] water_intake = [litres_of_water_consumed1, litres_of_water_consumed2, litres_of_water_consumed3, litres_of_water_consumed4, litres_of_water_consumed5] plt.plot(hours, water_intake) plt.title('Your water intake') plt.xlabel('hours') plt.ylabel('litres of water consumed')
但是我想通过脚本保存绘图而不是使用plot.show()并通过Matplotlib窗口保存绘图图像。
有没有办法这样做?
admin 更改状态以发布 2023年5月24日
如果需要,您可以从matplotlib
绘图中获取图像作为numpy数组图像:
ax.axis('off') fig.tight_layout(pad=0) # To remove the huge white borders ax.margins(0) fig.canvas.draw() image_from_plot = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8) image_from_plot = image_from_plot.reshape(fig.canvas.get_width_height()[::-1] + (3,))