我正在使用TextBox
模块中的matplotlib.widgets
。基本上,我的代码创建了一个带有文本框的图表,用户可以在其中输入要在控制台上打印的文本。
问题是只要我没有在文本框中输入任何数字,我就可以以交互方式拖动和放大绘图。如果我输入一个数字,那么情节就会停止交互。如果我输入字母,上述情况就不会发生。请使用下面的代码自行尝试。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.widgets as mwidgets
# Data
x = np.arange(0,2*np.pi, 0.01)
y = np.sin(x)
# Plot
fig = plt.figure()
ax = plt.axes()
ax.plot(x, y)
# Ask for text input on the plot display
def submitText(text):
print('Entered text = {}'.format(text))
axbox = plt.axes([0.3, 0.2, 0.3, 0.05])
text_box = mwidgets.TextBox(axbox, 'Write a text here: ', initial='{}'.format(text))
text_box.on_submit(submitText)
# Display plot
plt.show()
我真的需要输入数字而不是字母,并保持情节的互动行为。有谁知道为什么会这样?
任何建议都是受欢迎的。