抑制 MatPlotLib 良性警告

Suppressing a MatPlotLib benign warning

我正在绘制一些不同的图表,我不想有图例,因为我不需要它。

ax.scatter(x, y, ..., label='')

以上对我来说工作得很好,但它发出了一个 UserWarning。 我知道它是良性的,实际上根本不会影响程序,但我想找到一种方法来避免它。

我知道您可以抑制 python 中的所有警告,但我只想抑制这个特定的警告。

哦,删除 label='' 仍然会发出警告。

提前致谢

您可以禁止显示所有警告:

import warnings
warnings.filterwarnings("ignore")

或者您可以像这样抑制特定警告:

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