新闻动态

行业新闻企业新闻雷火电竞

Python Matplotlib 中如何用 plt.savefig 存储图片

雷火电竞

目录

前言

plt.show()展示图片的中何时候,截图进行保存,图片不是多么清晰 如何保存高清图也是一知识点

函数包名:import matplotlib.pyplot as plt

正文

主要功能:保存绘制数据后创建的图形。使用此方法可以将创建的存储图图形保存

函数源码:(根据需要进行选择)

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)

参数解释:

参数描述
fname指定格式图片或者指定文件位置
dpi画质
facecolor 和 edgecolor默认为白色
Orientation横向或者纵向
papertype纸张类型
format如png  、pdf
transparent图片背景透明
bbox_inches图表多余的中何空白区去除
pad_inches保存图形周围填充

正常保存:plt.savefig("xx.png"),也可以svg的格式进行保存

保存的时候需要plt.show()在plt.savefig()之后,顺序颠倒会出现图片为空白 。

当前文件保存:

注意事项:

要把所有的参数用上,可以用在直方图上

import matplotlib.pyplot as pltx =[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()

截图如下: chatgpt,AI,人工智能,聊天机器人,女友聊天机器人,在线ai对话,二次元聊天