Python/matplotlib : 解决matplotlib.mpl警告

16 投票
5 回答
30421 浏览
提问于 2025-04-18 11:40

我在用 Python 3.4 的时候使用 matplotlib 这个库。每次我启动程序时,都会看到下面的警告信息:

C:\Python34-32bits\lib\site-packages\matplotlib\cbook.py:123: MatplotlibDeprecationWarning: matplotlib.mpl 模块在 1.3 版本中被弃用了 请改用 import matplotlib as mpl 来代替。 warnings.warn(message, mplDeprecation, stacklevel=1)

据我所知,我并没有使用 mpl,而且我导入 matplotlib 的方式是:

import matplotlib.pyplot as plt
import matplotlib.animation as animation

我需要做些什么吗?

5 个回答

0

由于

MatplotlibDeprecationWarning: mplDeprecation 在 Matplotlib 3.6 中被弃用了,并将在之后的两个小版本中移除 ...

请改用以下内容:

import warnings
import matplotlib
warnings.filterwarnings("ignore", category=matplotlib.MatplotlibDeprecationWarning)
0

看到代码会很有帮助,不过记得先设置好图表的参数,然后再初始化图表

比如,你可能做过的事情:

plt.pcolormesh(X, Y, Z)
plt.axes().set_aspect('equal')

你需要做的事情:

plt.axes().set_aspect('equal')
plt.pcolormesh(X, Y, Z)
2

我用下面的代码成功地消除了那个警告:

import warnings

warnings.filterwarnings("ignore",category=UserWarning)
3

你可以在导入时暂时屏蔽一个警告

import warnings

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fxn()
41

你可以选择不显示那个特定的警告,这可能是比较推荐的做法:

import warnings
import matplotlib.cbook
warnings.filterwarnings("ignore",category=matplotlib.cbook.mplDeprecation)

撰写回答