打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Matplotlib数据可视化:图片展示与保存
import os
import matplotlib.image as mpimg
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']  # 中文字体支持

除了作图功能,matplotlib也提供本地图片展示以及保存图片的功能,这两个通能通过imshow()方法和savefig()方法实现。

1 图片展示

在使用imshow()展示图片前,需要先将图片读取出来。读取图片可以通过pillow库,也可以用matplotlib本身自带的image模块实现。

# 使用pillow库读取图片
img = Image.open(r"./jupyter/matplotlib/images/1.jpg")
fig = plt.figure(figsize=(84))
ax1 = fig.add_subplot(121)
ax1.imshow(img)

# 使用matplotlib自带image库读取图片
img = mpimg.imread(r"./jupyter/matplotlib/images/1.jpg")  
ax2 = fig.add_subplot(1,2,2)
ax2.imshow(img)


plt.show()

可以设置关闭坐标轴:

img = mpimg.imread(r"./jupyter/matplotlib/images/1.jpg")  
fig = plt.figure(figsize=(42))
ax1 = fig.add_subplot(111)
ax1.imshow(img)
ax1.axis('off')

plt.show()

imshow()方法中提供了众多参数以供个性化得展示图片,但我更建议使用pillow等专业的图片处理库先对图片进行处理,然后交由imshow()方法进行展示,以下是使用imshow方法显示灰度图片:

img=Image.open(r"./jupyter/matplotlib/images/1.jpg")
img = np.array(img)
if img.ndim == 3:
    img2 = img[:,:,0]
plt.subplot(321); plt.imshow(img)
plt.subplot(322); plt.imshow(img, cmap = 'gray')  # 无效
plt.subplot(323); plt.imshow(img2)
plt.subplot(324); plt.imshow(img2, cmap = 'gray')  # 灰度
plt.subplot(325); plt.imshow(img2, cmap = plt.cm.gray)  # 与cmap ='gray'等效
plt.subplot(326); plt.imshow(img2, cmap = plt.cm.gray_r)  # 反向灰度
plt.show()

显示多张图片:

index = 1

fig, axes = plt.subplots(43, figsize=(84), tight_layout=True)
for row in range(4):
    for col in range(3):
        image_name = os.path.join(r'./jupyter/matplotlib/images', str(index)+'.jpg')
        img = plt.imread(image_name)
        axes[row, col].imshow(img)
        axes[row, col].axis('off')
        index += 1
plt.show()

2 保存图片

当我们使用matplotlib完成作图后,难免有需要将图表保存到本地的需求,这时候就可以使用savefig()方法实现。savefig()方法主要参数如下:

  • fname:保存后图片名
  • dpi:像素
  • quality:用大于1小于100的标量表示图片质量
  • facecolor:前景色
  • edgecolor:边框颜色
  • format:文件格式
  • transparent:是否透明,当没有设置前景色和边框颜色时,如果transparent值为True保存为png格式时为透明
index = 1

fig, axes = plt.subplots(43, figsize=(84), tight_layout=True)
for row in range(4):
    for col in range(3):
        image_name = os.path.join(r'./jupyter/matplotlib/images', str(index)+'.jpg')
        img = plt.imread(image_name)
        axes[row, col].imshow(img)
        axes[row, col].axis('off')
        index += 1
fig.savefig('save_img.jpg', facecolor='grey', edgecolor='red')
plt.show()

这时候,在本地打开图片如下所示:

value= np.arange(6) ** 2
category = range(len(value))

fig = plt.figure(figsize=(84))

# 垂直柱状图
ax1 = fig.add_subplot(121)
ax1.set_title('图1 垂直柱状图')
ax1.bar(x=category, height=value)

# 垂直柱状图
ax2 = fig.add_subplot(122)
ax2.set_title('图2 水平柱状图')
ax2.barh(y=category, width=value)

fig.savefig(r"bar_img.png", transparent=True
plt.show()

保存后的本地图片bar_img.png如下所示:

作者:奥辰
Github:https://github.com/ChenHuabin321
https://www.cnblogs.com/chenhuabin


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
OpenCV+Python环境搭建以及第一个程序
Matplotlib image图像处理
用matplotlib中imshow()函数绘图
matplotlib.pyplot中绘画操作
python+opencv图像处理(十四)
使用 OpenCV 将图像转换为铅笔素描的 8 个步骤
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服