【问题标题】:Pyplot warning when evaluating expression from textbox widget从文本框小部件评估表达式时的 Pyplot 警告
【发布时间】:2021-05-24 20:51:21
【问题描述】:

如果我有一个带有多个“轴”(他们称之为)的 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()

【问题讨论】:

  • 在悬停其中一个图表时按 1 或 2 也会发生这种情况,即使没有 TextBox。
  • 我自己不会注意到的。为什么会这样?这些可能是一些键盘快捷键吗?如果是,如何解除绑定?
  • 目前还没有找到禁用它的好方法,但可以通过import warnings; warnings.simplefilter("ignore", matplotlib.MatplotlibDeprecationWarning) 快速修复来禁用此警告

标签: python matplotlib warnings axes


【解决方案1】:

看来你可以在 matplotlib 中取消默认键绑定:

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 - 您显然还可以指定要忽略的绑定。

另一种方法是取消此警告:

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 2015-11-05
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    相关资源
    最近更新 更多