Unable to use raw_input() during event handeling in Matplotlib while running pylab -- RuntimeError: can't re-enter readline
Question: I'm trying to write a script that allows the user to manipulate a graph via
event handling in matplotlib, but I need to have them enter some additional
information through the terminal

Calling `raw_input()` seems to break the scripts, and throws a `RuntimeError:
can't re-enter readline` error

here's a simple piece of code to demonstrate this:

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

This works fine if I run it using python, but breaks using ipython --pylab.
Unfortunately, I need the interactive mode

I see other people have had this problem but I haven't seen a solution


Answer: You're running into trouble because matplotlib is still listening for
keypresses. Unfortunately, simply disconnecting its event listening did not
work for me interactively. This solution, however, did work. Though it limits
you in not being able to use the 'y', 'e', 's', 'n', or 'o' keys. There are
workarounds for this if that is a necessity.

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

