Python?Matplotlib中使用plt.savefig存儲(chǔ)圖片的方法舉例
前言
plt.show()展示圖片的時(shí)候,截圖進(jìn)行保存,圖片不是多么清晰
如何保存高清圖也是一知識(shí)點(diǎn)
函數(shù)包名:import matplotlib.pyplot as plt
主要功能:
保存繪制數(shù)據(jù)后創(chuàng)建的圖形。使用此方法可以將創(chuàng)建的圖形保存
函數(shù)源碼:(根據(jù)需要進(jìn)行選擇)
savefig(fname, dpi=None, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None, metadata=None)
參數(shù)解釋:
| 參數(shù) | 描述 |
|---|---|
| fname | 指定格式圖片或者指定文件位置 |
| dpi | 畫(huà)質(zhì) |
| facecolor 和 edgecolor | 默認(rèn)為白色 |
| Orientation | 橫向或者縱向 |
| papertype | 紙張類型 |
| format | 如png、pdf |
| transparent | 圖片背景透明 |
| bbox_inches | 圖表多余的空白區(qū)去除 |
| pad_inches | 保存圖形周圍填充 |
正常保存:plt.savefig("xx.png"),也可以svg的格式進(jìn)行保存
保存的時(shí)候需要plt.show()在plt.savefig()之后,順序顛倒會(huì)出現(xiàn)圖片為空白。
當(dāng)前文件保存:
注意事項(xiàng):
- 如果plt.show() 在plt.savefig()前,就會(huì)導(dǎo)致保存圖片是空白的情況。
- window的路徑讀取,需要反斜杠
要把所有的參數(shù)用上,可以用在直方圖上
import matplotlib.pyplot as plt
x =[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
plt.hist(x)
plt.savefig("squares1.png",
bbox_inches ="tight",
pad_inches = 1,
transparent = True,
facecolor ="g",
edgecolor ='w',
orientation ='landscape')
plt.show()
截圖如下:

補(bǔ)充:解決plt.savefig() 保存多張圖片有重疊的問(wèn)題
問(wèn)題描述:
在多次調(diào)用plt.savefig()時(shí),出現(xiàn)了保存的圖片有上一個(gè)數(shù)據(jù)出現(xiàn)并重疊的現(xiàn)象。如下圖:

部分代碼:
import matplotlib.pyplot as plt
def ch_graph(num_clusters, ch_score, filepath, method, module):
# Plot ch graph
plt.plot(num_clusters, ch_score, 'bx-')
plt.xlabel('Number of cluster')
plt.ylabel('Calinski-Harabasz Score')
plt.title('Calinski-Harabasz Score against Number of Cluster')
plt.grid(True)
filename = 'ch_graph_one.png'
folder = 'Picture/'
ch_filepath = filepath + '/' + folder + filename
plt.savefig(ch_filepath)
def elbow_graph(num_clusters, Sum_of_squared_distances, filepath, method, module):
# Plot ch graph
plt.plot(num_clusters, Sum_of_squared_distances, 'bx-')
plt.xlabel('Number of cluster')
plt.ylabel('Sum of squared dist')
plt.title('Sum of squared dist against Number of Cluster')
plt.grid(True)
filename = 'elbow_graph_one.png'
folder = 'Picture/'
elbow_filepath = filepath + '/' + folder + filename
plt.savefig(elbow_filepath)
解決方法:
在plt.savefig()的下一行加上plt.close()就可以了。對(duì)于使用seaborn來(lái)繪制的圖片,也同樣使用plt.close()。
plt.close()內(nèi)可輸入的參數(shù)為:
- None: 目前的figure
- Figure: 給定的Figure實(shí)例
- int: 一個(gè) figure數(shù)
- str: 一個(gè) figure名字
- ‘all’: 全部 figures
另外,有時(shí)候也會(huì)因?yàn)闆](méi)有關(guān)閉上一個(gè)canvas, 導(dǎo)致出現(xiàn)以下問(wèn)題:
fig.canvas.draw_idle() # need this if 'transparent=True' to reset colors
總結(jié)
到此這篇關(guān)于Python Matplotlib中使用plt.savefig存儲(chǔ)圖片的文章就介紹到這了,更多相關(guān)Matplotlib用plt.savefig存儲(chǔ)圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python讀取nc數(shù)據(jù)并繪圖的方法實(shí)例
最近項(xiàng)目中需要處理和分析NC數(shù)據(jù),所以下面這篇文章主要給大家介紹了關(guān)于python讀取nc數(shù)據(jù)并繪圖的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
python 將md5轉(zhuǎn)為16字節(jié)的方法
今天小編就為大家分享一篇python 將md5轉(zhuǎn)為16字節(jié)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Python內(nèi)建序列通用操作6種實(shí)現(xiàn)方法
這篇文章主要介紹了Python內(nèi)建序列通用操作6種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
python+html實(shí)現(xiàn)前后端數(shù)據(jù)交互界面顯示的全過(guò)程
最近項(xiàng)目中采用了前后端分離的技術(shù),感覺(jué)有必要給大家總結(jié)下,所以下面這篇文章主要給大家介紹了關(guān)于python+html實(shí)現(xiàn)前后端數(shù)據(jù)交互界面顯示的相關(guān)資料,需要的朋友可以參考下2022-06-06
python 基于PYMYSQL使用MYSQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了python 基于PYMYSQL使用MYSQL數(shù)據(jù)庫(kù)的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12

