python绘图保存
时间: 2023-10-31 11:56:53 浏览: 104
要在Python中进行绘图并保存,可以使用Matplotlib和Seaborn这两个第三方库。下面是两种常用的方法:
方法一:
```
import seaborn as sns
import matplotlib.pyplot as plt
# 绘图
x = np.array([[1,2,3,4], [2,3,4,6], [10,2,3,6], [8,9,7,3]])
fig = sns.heatmap(x, annot=True)
# 保存图像
fig_name = 'heatmap.png'
fig_path = filepath + '/' + fig_name
fig.savefig(fig_path, dpi=400)
```
方法二:
```
import seaborn as sns
import matplotlib.pyplot as plt
# 绘图
x = np.array([1, 2, 3, 4])
fig = sns.barplot(x)
# 获取图像并保存
fig_name = 'barplot.png'
fig_path = filepath + '/' + fig_name
figure1 = fig.get_figure()
figure1.savefig(fig_path, dpi=300)
```
以上代码中,首先使用相应库绘制图形,然后使用`savefig`函数保存图像。在保存图像时,可以指定保存的文件名、路径和分辨率等参数。其中,`dpi`参数表示每英寸点数,值越高则分辨率越高。
这两种方法均适用于Matplotlib和Seaborn库。方法一更为方便,而方法二则可以通过获取图形对象来进行保存。
阅读全文
相关推荐


















