Disable MatPlotLib warning when using pyplot.imshow

时间:2019-02-24 03:02:10

标签: matplotlib

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

"Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers)."

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.

1 个答案:

答案 0 :(得分:0)

尝试一下:

import warnings

# do all your preprocessing here...

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

# do everything immediately after here...

我不确定这是否行得通(我希望将其作为注释,但是您不能在注释中进行代码块处理)

edit:在浏览matplotlib代码之后,我认为上面的命令不起作用的原因是实际上它不是警告,而是日志消息。因此,要使用的正确接口是logging

新解决方案:

import logging

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

# plotting code here

logger.setLevel(old_level)
相关问题