使用pyplot.imshow时禁用MatPlotLib警告 [英] Disable MatPlotLib warning when using pyplot.imshow

查看:104
本文介绍了使用pyplot.imshow时禁用MatPlotLib警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次来!使用 pyplot.imshow 函数时,我收到以下警告:

First time here! I get the following warning when I use the pyplot.imshow function:

使用RGB数据将输入数据剪切到有效显示范围(对于浮点数为[0..1],对于整数为[0..255])."

鉴于我的数据,我知道这是完全预期的行为.如何关闭此警告?我尝试过

I know this is a completely expected behavior, given my data. How do I turn this warning off? I have tried

import warnings
warnings.filterwarnings('ignore')

应会关闭所有警告,但由于某种原因,它对处理此特定警告没有帮助.

which should turn off all warnings, but for some reason it doesn't help with this particular warning.

如果这是重复的,请告诉我.谢谢.

Please let me know if this is a duplicate. Thanks.

推荐答案

尝试一下:

import warnings

# do all your preprocessing here...

with warnings.catch_warnings():
    # ...do only the stuff that causes warnings here...

# do everything immediately after here...

我不确定这是否可行(我希望将其作为评论,但您不能在评论中添加代码块)

I am not sure if this will work (I would have liked it as a comment, but you can't do code blocks in comments)

edit:遍历 matplotlib 代码后,我认为上述方法不起作用的原因实际上不是警告,而是日志消息.因此,要使用的正确界面是 logging 之一.

edit: After trawling through the matplotlib code, I think the reason the above doesn't work is that it is not, in fact, a warning, but rather a log message. Accordingly, the correct interface to use is the logging one.

新解决方案:

import logging

logger = logging.getLogger()
old_level = logger.level
logger.setLevel(100)

# plotting code here

logger.setLevel(old_level)

这篇关于使用pyplot.imshow时禁用MatPlotLib警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆