python - 从文本框小部件评估表达式时出现 Pyplot 警告

标签 python matplotlib warnings axes

如果我有一个带有多个“轴”(他们这样调用它们)的 pyplot 图,并且其中一个中有一个文本框,那么在编写一些特殊的字符序列时(例如 *2 ) 我收到一条警告,内容如下:

MatplotlibDeprecationWarning: Toggling axes navigation from the keyboard is deprecated since 3.3 and will be removed two minor releases later.
  return self.func(*args)

请注意,如果我只有一个轴,这似乎不会发生。

我需要使用这样的文本框来插入一个将被评估的函数,所以我可能需要使用 ***。是什么导致了这个警告?

这是一个重现场景的最小示例:

import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox

fig, (ax1, ax2) = plt.subplots(1, 2)
tb1 = TextBox(ax1, 'Textbox: ')
ax2.plot([1,2,3,4,5])
plt.show()

最佳答案

看来您可以在 matplotlib 中取消绑定(bind)默认键绑定(bind):

import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox

fig, (ax1, ax2) = plt.subplots(1, 2)
fig.canvas.mpl_disconnect(fig.canvas.manager.key_press_handler_id)
tb1 = TextBox(ax1, 'Textbox: ')
ax2.plot([1,2,3,4,5])
plt.show()

More info here - 您显然还可以指定要忽略的绑定(bind)。

另一种方法是只抑制此警告:

import warnings
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox

with warnings.catch_warnings():
    warnings.simplefilter("ignore", matplotlib.MatplotlibDeprecationWarning)
    fig, (ax1, ax2) = plt.subplots(1, 2)
    tb1 = TextBox(ax1, 'Textbox: ')
    ax2.plot([1,2,3,4,5])
    plt.show()

关于python - 从文本框小部件评估表达式时出现 Pyplot 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67678828/

相关文章:

Python日期时间到纪元

python - 如何从字典中制作分组的 fiddle 图

python - Matplotlib:随时间叠加多个二维箭袋图

R 函数 : Print Warning Only on First Call of Function

python - 如何使用 flask 显示目录中的所有图像

python - N 个随机、连续且不重叠的子序列,每个子序列的长度

python - 路由 GAE webapp2 中的可选 URL 参数

linux - 为 Python/Matplotlib 禁用 OpenGL

ruby-on-rails - 如何使用 Ruby 2.7.0 修复 Rails 的警告消息

perl:为什么 $hashsize = keys $hash{$foo} 会给出实验警告,我该如何写得更好?