網(wǎng)站首頁 編程語言 正文
前言
plt.show()展示圖片的時候,截圖進行保存,圖片不是多么清晰
如何保存高清圖也是一知識點
函數(shù)包名:import matplotlib.pyplot as plt
主要功能:
保存繪制數(shù)據(jù)后創(chuàng)建的圖形。使用此方法可以將創(chuàng)建的圖形保存
函數(shù)源碼:(根據(jù)需要進行選擇)
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 | 畫質 |
facecolor 和 edgecolor | 默認為白色 |
Orientation | 橫向或者縱向 |
papertype | 紙張類型 |
format | 如png、pdf |
transparent | 圖片背景透明 |
bbox_inches | 圖表多余的空白區(qū)去除 |
pad_inches | 保存圖形周圍填充 |
正常保存:plt.savefig("xx.png")
,也可以svg的格式進行保存
保存的時候需要plt.show()在plt.savefig()之后,順序顛倒會出現(xiàn)圖片為空白。
當前文件保存:
注意事項:
- 如果plt.show() 在plt.savefig()前,就會導致保存圖片是空白的情況。
- 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()
截圖如下:
補充:解決plt.savefig() 保存多張圖片有重疊的問題
問題描述:
在多次調(diào)用plt.savefig()時,出現(xiàn)了保存的圖片有上一個數(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()就可以了。對于使用seaborn來繪制的圖片,也同樣使用plt.close()。
plt.close()內(nèi)可輸入的參數(shù)為:
- None: 目前的figure
- Figure: 給定的Figure實例
- int: 一個 figure數(shù)
- str: 一個 figure名字
- ‘a(chǎn)ll’: 全部 figures
另外,有時候也會因為沒有關閉上一個canvas, 導致出現(xiàn)以下問題:
fig.canvas.draw_idle() # need this if 'transparent=True' to reset colors
總結
原文鏈接:https://blog.csdn.net/weixin_47872288/article/details/128739356
相關推薦
- 2022-04-09 python中異常的傳播詳解_python
- 2023-05-06 MacOS安裝python報錯"zsh:?command?not?found:python"的解決方
- 2022-04-08 IIS部署ASP.NET?Core項目及常見問題總結_基礎應用
- 2022-09-10 Go語言中的IO操作及Flag包的用法_Golang
- 2022-04-19 C語言內(nèi)存管理及初始化細節(jié)示例詳解_C 語言
- 2024-02-28 UNI-APP,動態(tài)設置view的背景圖片
- 2022-11-04 React組件化學習入門教程講解_React
- 2022-09-08 Python報錯SyntaxError:unexpected?EOF?while?parsing的解
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支