如果我有一个带有多个“轴”(他们这样调用它们)的 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/