How to disable the keyboard shortcuts in Matplotlib?

To disable keyboard shortcuts in Matplotlib, we can use the remove() method on the plt.rcParams keymap settings. This is useful when you want to prevent accidental triggering of default shortcuts or customize the interface behavior.

Disabling a Single Shortcut

Let's disable the 's' key shortcut that normally saves the figure −

import numpy as np
import matplotlib.pyplot as plt

# Configure figure settings
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Remove the 's' key from save shortcut
plt.rcParams['keymap.save'].remove('s')

# Create sample data
n = 10
x = np.random.rand(n)
y = np.random.rand(n)

# Plot the data
plt.plot(x, y, 'bo-')
plt.title('Plot with Disabled Save Shortcut')
plt.xlabel('X values')
plt.ylabel('Y values')

plt.show()

Disabling Multiple Shortcuts

You can disable multiple keyboard shortcuts by targeting different keymap settings −

import numpy as np
import matplotlib.pyplot as plt

# Disable multiple shortcuts
plt.rcParams['keymap.save'] = []        # Remove all save shortcuts
plt.rcParams['keymap.quit'] = []        # Remove quit shortcuts
plt.rcParams['keymap.fullscreen'] = []  # Remove fullscreen shortcuts

# Create sample data
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

plt.figure(figsize=(8, 4))
plt.plot(x, y, 'r-', linewidth=2)
plt.title('Plot with Multiple Disabled Shortcuts')
plt.grid(True, alpha=0.3)

plt.show()

Common Keyboard Shortcuts

Keymap Setting Default Keys Function
keymap.save s, ctrl+s Save figure
keymap.quit q, ctrl+w Close figure
keymap.home h, r, home Reset view
keymap.back left, c, backspace Previous view
keymap.forward right, v Next view

Restoring Default Shortcuts

To restore shortcuts after disabling them, you need to reassign the default values −

import matplotlib.pyplot as plt

# First disable the shortcut
plt.rcParams['keymap.save'] = []

# Later restore it with default values
plt.rcParams['keymap.save'] = ['s', 'ctrl+s']

print("Save shortcut restored:", plt.rcParams['keymap.save'])
Save shortcut restored: ['s', 'ctrl+s']

Conclusion

Use plt.rcParams['keymap.name'].remove('key') to disable specific shortcuts or assign an empty list to disable all shortcuts for a particular function. This provides better control over the Matplotlib interface behavior.

Updated on: 2026-03-25T22:35:09+05:30

771 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements