Disable default arrow key bindings matplotlib

Using the matplotlib function mpl_connect, you can associate events with function calls. However, the left and right arrow keys are by default tied back and forth in the figure history. I would like to disable this binding by default.

For instance:

import matplotlib.pyplot as plt

def on_key_press(event):
    if event.key == "left":
        print("Left!")
    elif event.key == "right":
        print("Right!")

plt.plot([0, 1, 2, 3, 4], [5, 2, 1, 2, 5])
plt.gcf().canvas.mpl_connect("key_press_event", on_key_press)

Pressing the left key now displays Left!on the console. However, when we get closer to the figure, the left key will also be “backward” and zoom out. (The right key will go forward and come back again.) I would like this not to happen - how do I do this? Creating on_key_pressreturn Falsedoes not do the trick.

( : , , , , ( [0,1,2,3,4] [ 5,2,1,2,5]), , . / , - , .)

+4
1

fig = plt.gcf()
fig.canvas.mpl_disconnect(fig.canvas.manager.key_press_handler_id)

, . , rcparams,

import matplotlib as mpl
mpl.rcParams['keymap.back'].remove('left')
mpl.rcParams['keymap.forward'].remove('right')

'left' 'right'.

remove, , try...except, , (, ). , rcparams, , , .

+4

All Articles