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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: se in mpl 3.5 and mpl_toolkits.mplot3d.axes3d will no longer support the auto_add_to_figure parameter. 这是matplotlib库的一个警告,意思是在3.4版本之后,使用axes3d(fig)将自身添加到图形中已经被弃用了。现在需要传递关键字参数auto_add_to_figure=false,并使用fig.add_axes(ax)来抑制此警告。auto_add_to_figure的默认值将在mpl 3.5中更改为false,而mpl_toolkits.mplot3d.axes3d将不再支持auto_add_to_figure参数。 ### 回答2: matplotlibdeprecationwarning: axes3d(fig)添加自己到图形中已经过时,自3.4版本开始,传递关键字参数auto_add_to_figure=false并使用fig.add_axes(ax)来抑制此警告。 自动将auto_add_to_figure的默认值更改为fal。 这段警告信息是关于matplotlib库中使用axes3d函数时的警告信息。它表明,如果你使用axes3d函数来创建3D图形,则该函数会自动将其添加到图形中。然而,这种行为已经过时,并且在未来的版本中将被移除。因此,为了使你的代码不受影响,建议使用新的方法来添加3D图形。 更具体地说,你可以通过将关键字参数auto_add_to_figure设置为false来抑制警告信息,并使用fig.add_axes(ax)方法来手动将3D图形添加到图形中。这样,你就可以使用新的方法添加3D图形,而不会受到警告信息的影响。 总的来说,这段警告信息告诉我们,matplotlib库正在发展和更新。因此,当我们使用matplotlib提供的函数时,我们必须时刻关注它们的更新和变化,以避免在未来的版本中遇到问题。所以,我们应该牢记这些警告信息,并尽可能遵循最新的方法和技术,来创建优秀的可视化效果。 ### 回答3: matplotlib是一种Python下广泛使用的绘图库,它支持多种绘图方式,包括2D和3D绘图。在使用matplotlib进行3D绘图时,可能会遇到一个叫做“axes3d(fig) adding itself to the figure is deprecated since 3.4”的警告信息。 警告信息的意思是,使用axes3d(fig)将自身添加到图形中的做法已经不再被建议使用,因为这会导致一些不必要的问题。从matplotlib 3.4版本开始,警告信息的默认值将更改为false。因此,建议我们在使用axes3d时,传递一个关键字参数auto_add_to_figure=false,以及使用fig.add_axes(ax)的方式来避免这个警告信息。 简单来说,这个警告信息是为了提醒我们在使用matplotlib进行3D绘图时要注意不必要的自动添加问题。如果我们忽略这个警告信息,可能会导致一些意想不到的问题。因此,我们需要及时采取措施,对警告信息进行有效的处理。 总之,对于matpoltlib的3D绘图,如果出现“axes3d(fig) adding itself to the figure is deprecated since 3.4”的警告信息,我们应该注意到这个问题,并及时采取措施以避免出现不必要的后果。同时,我们还应该对matplotlib的相关文档进行深入的学习,以便更好地了解该库的使用方法和注意事项。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值