【matplotlib】可视化解决方案——如何正确展示和保存图像

概述

在 matplotlib 实践中,很多情形下需要对输出图形进行指定路径的有效保存。这样输出图形就可以用于印刷或者其他形式的展示。因此本文就来介绍一下保存输出图形的方法。
一般情况下可以采用两种方式保存图像,第一种是在输出界面点击保存按钮;第二种是通过执行代码进行输出图形的保存。

示例

使用保存按钮进行保存

我们以下面的代码为例演示如何使用按钮保存图形。

import matplotlib.pyplot as plt  
import numpy as np  
  
plt.rcParams['axes.facecolor'] = "lemonchiffon"  
  
x = np.linspace(0, 2, 1000)  
y = 0.9 * np.sin(np.pi * x)  
fig, ax = plt.subplots(2, 3)  
  
# subplot(2,3,1)  
ax[0, 0].plot(x, y, lw=3, color="steelblue")  
ax[0, 0].spines["right"].set_visible(False)  
ax[0, 0].spines["top"].set_visible(False)  
  
# set left and bottom spines position  
ax[0, 0].spines["left"].set_position(("data", 0.5))  
ax[0, 0].spines["bottom"].set_position(("data", 1))  
  
# set tickline position of bottom and left spines  
ax[0, 0].xaxis.set_ticks_position("bottom")  
ax[0, 0].yaxis.set_ticks_position("left")  
ax[0, 0].set_ylim(-1, 1)  
  
# subplot(2,3,4)  
ax[1, 0].plot(x, y, lw=3, color="steelblue")  
ax[1, 0].spines["right"].set_color("none")  
ax[1, 0].spines["top"].set_color("none")  
  
# set left and bottom spines position  
ax[1, 0].spines["left"].set_position("zero")  
ax[1, 0].spines["bottom"].set_position("zero")  
  
# set tickline position of bottom and left spines  
ax[1, 0].xaxis.tick_bottom()  
ax[1, 0].yaxis.tick_left()  
ax[1, 0].set_ylim(-1, 1)  
  
# subplot(2,3,2)  
ax[0, 1].plot(x, y, lw=3, color="steelblue")  
ax[0, 1].spines["right"].set_visible(False)  
ax[0, 1].spines["top"].set_visible(False)  
  
# set left and bottom spines position  
ax[0, 1].spines["left"].set_position(("axes", 0.25))  
ax[0, 1].spines["bottom"].set_position(("axes", 0.75))  
  
# set tickline position of bottom and left spines  
ax[0, 1].xaxis.set_ticks_position("bottom")  
ax[0, 1].yaxis.set_ticks_position("left")  
ax[0, 1].set_ylim(-1, 1)  
  
# subplot(2,3,5)  
ax[1, 1].plot(x, y, lw=3, color="steelblue")  
  
ax[1, 1].spines["right"].set_color("none")  
ax[1, 1].spines["top"].set_color("none")  
ax[1, 1].spines["left"].set_position("center")  
  
# set left and bottom spines position  
ax[1, 1].spines["bottom"].set_position("center")  
  
# set tickline position of bottom and left spines  
ax[1, 1].xaxis.tick_bottom()  
ax[1, 1].yaxis.tick_left()  
ax[1, 1].set_ylim(-1, 1)  
  
# subplot(2,3,3)  
ax[0, 2].plot(x, y, lw=3, color="steelblue")  
ax[0, 2].spines["right"].set_visible(False)  
ax[0, 2].spines["top"].set_visible(False)  
  
# set leftandbottomspinespines position  
ax[0, 2].spines["left"].set_position(("outward", 3))  
ax[0, 2].spines["bottom"].set_position(("outward", 2))  
  
# set tickline position of bottom and left spines  
ax[0, 2].xaxis.set_ticks_position("bottom")  
ax[0, 2].yaxis.set_ticks_position("left")  
ax[0, 2].set_ylim(-1, 1)  
  
# subplot(2,3,6)  
ax[1, 2].plot(x, y, lw=3, color="steelblue")  
ax[1, 2].spines["right"].set_color("none")  
ax[1, 2].spines["top"].set_color("none")  
  
# set left and bottom spines position  
ax[1, 2].spines["left"].set_position(("outward", -3))  
ax[1, 2].spines["bottom"].set_position(("outward", -2))  
  
# set tickline position of bottom and left spines  
  
ax[1, 2].xaxis.tick_bottom()  
ax[1, 2].yaxis.tick_left()  
  
ax[1, 2].set_ylim(-1, 1)  
  
fig.subplots_adjust(wspace=0.35, hspace=0.2)  
plt.show()

画图结果如下:

在这里插入图片描述

可以通过下图中红框位置的保存按钮进行保存。

在这里插入图片描述

保存支持的格式如下:

在这里插入图片描述

通过执行代码语句保存图形

通过执行代码语句保存图形其实很简单,依然以上面的代码为例,只需在代码的最后添加 plt.savefig("img.png") 就可以完成图片保存行为,完整代码如下:

import matplotlib.pyplot as plt  
import numpy as np  
  
plt.rcParams['axes.facecolor'] = "lemonchiffon"  
  
x = np.linspace(0, 2, 1000)  
y = 0.9 * np.sin(np.pi * x)  
fig, ax = plt.subplots(2, 3)  
  
# subplot(2,3,1)  
ax[0, 0].plot(x, y, lw=3, color="steelblue")  
ax[0, 0].spines["right"].set_visible(False)  
ax[0, 0].spines["top"].set_visible(False)  
  
# set left and bottom spines position  
ax[0, 0].spines["left"].set_position(("data", 0.5))  
ax[0, 0].spines["bottom"].set_position(("data", 1))  
  
# set tickline position of bottom and left spines  
ax[0, 0].xaxis.set_ticks_position("bottom")  
ax[0, 0].yaxis.set_ticks_position("left")  
ax[0, 0].set_ylim(-1, 1)  
  
# subplot(2,3,4)  
ax[1, 0].plot(x, y, lw=3, color="steelblue")  
ax[1, 0].spines["right"].set_color("none")  
ax[1, 0].spines["top"].set_color("none")  
  
# set left and bottom spines position  
ax[1, 0].spines["left"].set_position("zero")  
ax[1, 0].spines["bottom"].set_position("zero")  
  
# set tickline position of bottom and left spines  
ax[1, 0].xaxis.tick_bottom()  
ax[1, 0].yaxis.tick_left()  
ax[1, 0].set_ylim(-1, 1)  
  
# subplot(2,3,2)  
ax[0, 1].plot(x, y, lw=3, color="steelblue")  
ax[0, 1].spines["right"].set_visible(False)  
ax[0, 1].spines["top"].set_visible(False)  
  
# set left and bottom spines position  
ax[0, 1].spines["left"].set_position(("axes", 0.25))  
ax[0, 1].spines["bottom"].set_position(("axes", 0.75))  
  
# set tickline position of bottom and left spines  
ax[0, 1].xaxis.set_ticks_position("bottom")  
ax[0, 1].yaxis.set_ticks_position("left")  
ax[0, 1].set_ylim(-1, 1)  
  
# subplot(2,3,5)  
ax[1, 1].plot(x, y, lw=3, color="steelblue")  
  
ax[1, 1].spines["right"].set_color("none")  
ax[1, 1].spines["top"].set_color("none")  
ax[1, 1].spines["left"].set_position("center")  
  
# set left and bottom spines position  
ax[1, 1].spines["bottom"].set_position("center")  
  
# set tickline position of bottom and left spines  
ax[1, 1].xaxis.tick_bottom()  
ax[1, 1].yaxis.tick_left()  
ax[1, 1].set_ylim(-1, 1)  
  
# subplot(2,3,3)  
ax[0, 2].plot(x, y, lw=3, color="steelblue")  
ax[0, 2].spines["right"].set_visible(False)  
ax[0, 2].spines["top"].set_visible(False)  
  
# set leftandbottomspinespines position  
ax[0, 2].spines["left"].set_position(("outward", 3))  
ax[0, 2].spines["bottom"].set_position(("outward", 2))  
  
# set tickline position of bottom and left spines  
ax[0, 2].xaxis.set_ticks_position("bottom")  
ax[0, 2].yaxis.set_ticks_position("left")  
ax[0, 2].set_ylim(-1, 1)  
  
# subplot(2,3,6)  
ax[1, 2].plot(x, y, lw=3, color="steelblue")  
ax[1, 2].spines["right"].set_color("none")  
ax[1, 2].spines["top"].set_color("none")  
  
# set left and bottom spines position  
ax[1, 2].spines["left"].set_position(("outward", -3))  
ax[1, 2].spines["bottom"].set_position(("outward", -2))  
  
# set tickline position of bottom and left spines  
  
ax[1, 2].xaxis.tick_bottom()  
ax[1, 2].yaxis.tick_left()  
  
ax[1, 2].set_ylim(-1, 1)  
  
fig.subplots_adjust(wspace=0.35, hspace=0.2)  
  
plt.savefig("img.png")  
plt.show()

这里我们稍微深入研究一下 plt.savefig() 这个方法,它的源码分析如下:

def savefig(*args, **kwargs):  
	# 获取当前的Figure对象
    fig = gcf()  
    # 调用Figure对象的savefig方法
    res = fig.savefig(*args, **kwargs)  
    fig.canvas.draw_idle()   # need this if 'transparent=True' to reset colors  
    return res

从源码可以看到,matplotlib 只能以一个 Figure 对象为单位进行保存,所以不能将子图单独保存,这提醒我们在布局阶段就得想好是否对 Figure 对象划分子图;其次源码中的 Figure 对象是通过 gcf() 方法获取的,如果程序中有多个 Figure 对象,有可能保存这个行为和预期不符,建议在程序中不要调用 plt.savefig() 方法,而是使用面向对象的方式,调用 Figure.savefig() 进行图像保存。

除了传递图片名称(fname),savefig 方法还可以传递如下参数:

  • dpi:图像分辨率,默认和调用方法的 Figure 对象一致;
  • format:如果图片名称中没有指定格式,则使用该参数指定的格式保存图片;
  • metadata:图片元数据,该参数的行为取决于图像格式;
  • facecolor:指定保存图片的背景颜色;
  • edgecolor:指定保存图片的边缘颜色;

往期回顾

  1. 【matplotlib】可视化解决方案——如何设置字符串的输出字体效果
  2. 【matplotlib】可视化解决方案——如何正确使用颜色映射表
  3. 【matplotlib】可视化解决方案——如何调整轴脊位置
  4. 【matplotlib】可视化解决方案——如何设置坐标系计量方法
  5. 【matplotlib】可视化解决方案——如何正确使用文本注释
  6. 【matplotlib】可视化解决方案——如何调整计量单位和计量方法
  7. 【matplotlib】可视化解决方案——如何实现图形的动画效果
  8. 【matplotlib】可视化解决方案——如何正确使用plot方法
  9. 【matplotlib】可视化解决方案——如何正确使用bar方法
  10. 【matplotlib】可视化解决方案——如何向画布中添加坐标轴

文中难免会出现一些描述不当之处(尽管我已反复检查多次),欢迎在留言区指正,相关的知识点也可进行分享,希望大家都能有所收获!!如果觉得我的文章写得还行,不妨支持一下。你的每一个转发、关注、点赞、评论都是对我最大的支持!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小猪猪家的大猪猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值