简体   繁体   English

如何抑制 matplotlib 警告?

[英]How to suppress matplotlib warning?

I am getting an warning from matplotlib every time I import pandas :每次import pandas时,都会收到来自matplotlib的警告:

/usr/local/lib/python2.7/site-packages/matplotlib/__init__.py:872: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.


 warnings.warn(self.msg_depr % (key, alt_key))

What is the best way to suppress it?抑制它的最好方法是什么? All packages are up-to-date.所有软件包都是最新的。

Conf: OSX with a brew Python 2.7.10 (default, Jul 13 2015, 12:05:58), and pandas==0.17.0 and matplotlib==1.5.0 Conf: OSX with a brew Python 2.7.10 (default, Jul 13 2015, 12:05:58), and pandas==0.17.0 and matplotlib==1.5.0

You can suppress all warnings:您可以禁止所有警告:

import warnings
warnings.filterwarnings("ignore")

import pandas

You can either suppress the warning messages as suggested by AndreL or you can resolve this specific issue and stop getting the warning message once and for all.您可以按照 AndreL 的建议取消警告消息,也可以解决此特定问题并一劳永逸地停止接收警告消息。 If you want the latter, do the following.如果您想要后者,请执行以下操作。

Open your matplotlibrc file and search for axes.color_cycle .打开您的matplotlibrc文件并搜索axes.color_cycle If you're getting the warning message it means that your matplotlibrc file should show something like this:如果您收到警告消息,则意味着您的matplotlibrc文件应显示如下内容:

axes.color_cycle : b, g, r, c, m, y, k  # color cycle for plot lines

You should replace that line by this:您应该用以下内容替换该行:

axes.prop_cycle : cycler('color', ['b', 'g', 'r', 'c', 'm', 'y', 'k'])

And the warning message should be gone.警告信息应该消失了。

Rather than hiding everything, you can also hide specific warnings.您还可以隐藏特定警告,而不是隐藏所有内容。 For instance if we want to hide only matplotlib warnings:例如,如果我们只想隐藏matplotlib警告:

warnings.filterwarnings( "ignore", module = "matplotlib\..*" )

The filter can be customised down to the exact message and line number of the file in which the warning originates, let's say if it's just one warning that annoys you and not matplotlib as a whole.可以将过滤器自定义为发出警告的文件的确切消息和行号,假设它只是一个让您烦恼的警告,而不是整个matplotlib See https://docs.python.org/3/library/warnings.html for more details.有关更多详细信息,请参阅https://docs.python.org/3/library/warnings.html

You can suppress the warning UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.您可以取消警告UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter. UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter. by using prop_cycle at the appropriate place.通过在适当的地方使用prop_cycle

For example, in the place you had used color_cycle :例如,在您使用color_cycle的地方:

matplotlib.rcParams['axes.color_cycle'] = ['r', 'k', 'c']

Replace it with the following:将其替换为以下内容:

matplotlib.rcParams['axes.prop_cycle'] = mpl.cycler(color=["r", "k", "c"]) 

For a greater glimpse, here is an example:为了更深入地了解,这里有一个例子:

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=["r", "k", "c"]) 

x = np.linspace(0, 20, 100)

fig, axes = plt.subplots(nrows=2)

for i in range(10):
    axes[0].plot(x, i * (x - 10)**2)

for i in range(10):
    axes[1].plot(x, i * np.cos(x))

plt.show()

在此处输入图片说明

如果你正在使用日志模块,试试这个: logging.getLogger('matplotlib').setLevel(level=logging.CRITICAL)

降级到matplotlib 1.4.3之前的稳定版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM