MatplotlibDeprecationWarning: The 'hold' keyword argument is deprecated since 2.0.mplDeprecation)

使用 python 画图像的 R、G、B 三个通道直方图的时候,我调用了 matplotlib 这个库函数,代码如下:

# -*- coding: UTF-8 -*-
import numpy as np
import cv2
import matplotlib.pyplot as plt
def drawHistogram(img) :
    B, G, R = cv2.split(img)
    b = np.array(B).flatten()
    g = np.array(G).flatten()
    r = np.array(R).flatten()
    # 蓝色通道的直方图
    plt.title("Histogram of Blue Channel")
    plt.hist(b, bins=256, label = "blue", density=1, facecolor='green', edgecolor='b', alpha=0.75)
    plt.show()
    # 绿色通道的直方图
    plt.title("Histogram of Green Channel")
    plt.hist(g, bins=256, label = "green", density=1, facecolor='green', edgecolor='g', alpha=0.75)
    plt.show()
    # 红色通道的直方图
    plt.title("Histogram of Red Channel")
    plt.hist(r, bins=256, label = "red", density=1, facecolor='green', edgecolor='g', alpha=0.75)
    plt.show()

由于想让直方图是叠加在一起的,就传入了 hold 参数。即,将上面的 hist 函数增加一个输入参数:

# -*- coding: UTF-8 -*-
import numpy as np
import cv2
import matplotlib.pyplot as plt
def drawHistogram(img) :
    B, G, R = cv2.split(img)
    b = np.array(B).flatten()
    g = np.array(G).flatten()
    r = np.array(R).flatten()
    # 蓝色通道的直方图
    plt.title("Histogram of Blue Channel")
    plt.hist(b, bins=256, label = "blue", density=1, facecolor='green', edgecolor='b', alpha=0.75, hold=1)
    plt.show()
    # 绿色通道的直方图
    plt.title("Histogram of Green Channel")
    plt.hist(g, bins=256, label = "green", density=1, facecolor='green', edgecolor='g', alpha=0.75, hold=1)
    plt.show()
    # 红色通道的直方图
    # plt.cla()  # 可以清空上面存留的信息,防止直方图叠加
    plt.title("Histogram of Red Channel")
    plt.hist(r, bins=256, label = "red", density=1, facecolor='green', edgecolor='g', alpha=0.75, hold=1)
    plt.show()

于是就出现了这个警告:

D:\Anaconda3\lib\site-packages\matplotlib\pyplot.py:3126: MatplotlibDeprecationWarning: The ‘hold’ keyword argument is deprecated since 2.0.
mplDeprecation)

解决方案

这个提示的信息,意思是说,在新版本的 matplotlib 中,这个参数已经被舍弃了。查看官方文档,有如下解释:

Setting or unsetting hold (deprecated in version 2.1) has now been completely removed. Matplotlib now always behaves as if hold=True. To clear an axes you can manually use cla(), or to clear an entire figure use clf().

他的意思是说,默认状态下,直方图已经是可以叠加的了。如果需要放弃叠加特性的话,就可以使用 cla() 或者 clf() 这两个函数,可以清空之前直方图的设置,就可以实现放弃叠加效果了。

自己亲自测试了一下,感觉还挺好用。

参考文档

API Changes — Matplotlib 3.0.2 documentation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值