简体   繁体   English

在 IPython 中抑制 Matplotlib 图例警告

[英]Suppress Matplotlib Legend Warning in IPython

I am creating a plot with a large amount of data and adding a legend to it:我正在创建一个包含大量数据的 plot 并向其添加图例:

import matplotlib.pyplot as plt
import numpy as np

plt.plot(np.arange(100000), np.random.random((100000, 10)), label='data')
plt.legend()

This generates a warning (as expected), when the legend actually gets drawn in another thread:当图例实际在另一个线程中绘制时,这会生成警告(如预期的那样):

<matplotlib.legend.Legend at 0x1b5824d04c8>C:\Users\...\lib\site-packages\ipykernel\eventloops.py:106: UserWarning: Creating legend with loc="best" can be slow with large amounts of data.
  app.exec_()

I would like to suppress this specific warning, so I do我想抑制这个特定的警告,所以我这样做

from warnings import filterwarnings
filterwarnings("ignore", category=UserWarning, module="matplotlib")

Running the same plotting code again produces the same warning.再次运行相同的绘图代码会产生相同的警告。

How do I suppress it?我该如何抑制它? Ideally, I would like to use warnings.catch_warnings around the call to legend .理想情况下,我想在对legend的调用周围使用warnings.catch_warnings Something like就像是

with warnings.catch_warnings():
    plt.legend()

The original commands were run in interactive mode in spyder (ipython) using a Qt5agg backend.原始命令使用Qt5agg后端在 spyder (ipython) 中以交互模式运行。 I also ran the same commands followed by plt.show() in a plain python console:我还在一个普通的 python 控制台中运行了相同的命令,然后是plt.show()

__main__:1: UserWarning: Creating legend with loc="best" can be slow with large amounts of data.

Running filterwarnings("ignore", category=UserWarning, module="__main__") helps, but only in non-interactive mode.运行filterwarnings("ignore", category=UserWarning, module="__main__")帮助,但仅限于非交互模式。 In interactive mode, the following warning is issued:在交互模式下,发出以下警告:

C:\Users\...\lib\site-packages\pyreadline\console\console.py:514: UserWarning: Creating legend with loc="best" can be slow with large amounts of data.
  call_function(inputHookFunc, ())

The issue seems to be ipython specific.这个问题似乎是特定于 ipython 的。 I am using我在用

Python 3.8.3, IPython 7.16.1, matplotlib 3.2.2 (, and numpy 1.18.5) Python 3.8.3、IPython 7.16.1、matplotlib 3.2.2(和 numpy 1.18.5)

This answer shows how to suppress the legend warning as per the OP heading.此答案显示了如何根据 OP 标题抑制图例警告。 It doesn't explain how to temporarily catch the warning once it has been issued.它没有解释如何在发出警告后暂时捕获警告。 If you're interested in the latter than the following is more a workaround than an answer.如果您对后者感兴趣,那么以下内容更像是一种解决方法而不是答案。

According to the source code the warning is shown if the default loaction is used (from rcParams['legend.loc'] which is 'best' by default) and it takes more than 1 sec to find the best location.根据源代码,如果使用默认位置(来自默认为'best'rcParams['legend.loc'] ,则会显示警告,并且需要超过 1 秒才能找到最佳位置。 If you explicitly set the location to 'best' no warning will be issued.如果您明确将位置设置为'best'则不会发出警告。

Tested on Windows, both IPython and plain command line (I had to increase the number on my machine):在 Windows 上测试,IPython 和普通命令行(我不得不增加我机器上的数字):

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(1)
plt.plot(np.arange(1_000_000), np.random.random((1_000_000, 10)), label='data')
plt.legend(loc='best')
plt.show()

As an alternative you could permanently suppress the warning by its message (instead of module ):作为替代方案,您可以通过其消息(而不是module )永久抑制警告:

filterwarnings("ignore", 'Creating legend with loc="best" can be slow with large amounts of data.')

The drawback is that it's permanent.缺点是它是永久性的。 I couldn't find a way to suppress the warning temporarily by using a context manage as the context of course closes long before the legend position calculation takes place.我找不到通过使用上下文管理暂时抑制警告的方法,因为上下文当然在图例 position 计算发生之前很久就关闭了。
To revert this change to the warnings filters list you can do one of the following after the legend is finally drawn: warnings.filters = [w for w in warnings.filters if not w[1] or not re.match(w[1], 'Creating legend with loc="best" can be slow with large amounts of data.')] or filterwarnings("default", 'Creating legend with loc="best" can be slow with large amounts of data.') .要将此更改恢复到警告过滤器列表,您可以在最终绘制图例执行以下操作之一: warnings.filters = [w for w in warnings.filters if not w[1] or not re.match(w[1], 'Creating legend with loc="best" can be slow with large amounts of data.')]filterwarnings("default", 'Creating legend with loc="best" can be slow with large amounts of data.') .

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

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