Python 运行pylab时,无法在Matplotlib中处理事件期间使用原始输入()——运行时错误:can';t重新输入readline
我正在尝试编写一个脚本,允许用户通过matplotlib中的事件处理操作图形,但我需要让他们通过终端输入一些附加信息 调用Python 运行pylab时,无法在Matplotlib中处理事件期间使用原始输入()——运行时错误:can';t重新输入readline,python,matplotlib,ipython,Python,Matplotlib,Ipython,我正在尝试编写一个脚本,允许用户通过matplotlib中的事件处理操作图形,但我需要让他们通过终端输入一些附加信息 调用raw\u input() 下面是一段简单的代码来演示这一点: import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111) ax.plot(np.random.rand(10)) def keypress(event): print
raw\u input()
下面是一段简单的代码来演示这一点:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))
def keypress(event):
print 'You press the "%s" key' %event.key
print 'is this true? Type yes or no'
y_or_n = raw_input()
cid = fig.canvas.mpl_connect('key_press_event', keypress)
plt.show()
如果我使用python运行它,这很好,但是使用ipython--pylab会中断。不幸的是,我需要交互模式
我看到其他人也有这个问题,但我还没有找到解决方案,因为matplotlib仍在监听按键,所以您遇到了麻烦。不幸的是,简单地断开其事件侦听对我来说并没有交互作用。然而,这个解决方案确实有效。尽管它限制了您不能使用“y”、“e”、“s”、“n”或“o”键。如果这是必要的话,有一些解决办法
import matplotlib.pyplot as plt
import numpy as np
#disable matplotlib keymaps
keyMaps = [key for key in plt.rcParams.keys() if 'keymap.' in key]
for keyMap in keyMaps:
plt.rcParams[keyMap] = ''
str = ''
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))
def keypress(event):
global str
if event.key in ['y','e','s','n','o']:
str += event.key
else:
print 'You press the "%s" key' %event.key
print 'is this true? Type yes or no'
if str == 'yes':
print str
str = ''
elif str == 'no':
print str
str = ''
cid = fig.canvas.mpl_connect('key_press_event', keypress)
plt.show()