当前位置: 代码迷 >> 综合 >> MatplotlibDeprecationWarning: The 'hold' keyword argument is deprecated since 2.0.mplDeprecation)
  详细解决方案

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

热度:17   发布时间:2023-12-15 17:10:17.0

使用 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

  相关解决方案