[英]How to save figure in Matplotlib in Python
我正在嘗試將 Matplotlib 中的圖形保存到文件中,但是當我運行命令保存圖像時,它沒有給出任何錯誤,但我看不到文件。
plt.savefig('Traveling Salesmen Graph.png')
pyplot 跟蹤“當前圖形”,並在庫上調用需要圖形的函數對其進行操作,但您也可以通過在圖形 object 上調用savefig()
來更明確。
以https://pythonspot.com/matplotlib-save-figure-to-image-file/ 為例:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
y = [2,4,6,8,10,12,14,16,18,20]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
plt.title('Legend inside')
ax.legend()
#plt.show()
fig.savefig('plot.png')
以這種方式明確應該可以解決您的問題。
有關在“當前圖”上運行的 pyplot 函數的參考,請參閱:https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.