Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

matplotlib keyboard event

# //TL;DR////
fig.canvas.mpl_connect('key_press_event', lambda e: print(f"the key '{e.key}' was pressed" ))
#/////

import matplotlib.pyplot as plt
import numpy as np

def on_press(event):
    if event.key == 'h':
        leg.set_visible(not leg.get_visible())
        plt.draw()

fig, ax1=plt.subplots()
t = np.linspace(0, 1)
for i in range(4):
    y1 = (2+i*2) * np.sin(2*np.pi*pow(2,i)*t)
    ax1.plot(t, y1, lw=2, label= f'{i+1} HZ')

leg=ax1.legend(loc="upper left")
fig.canvas.mpl_connect('key_press_event', on_press)
plt.title("press 'h' to hide legend")
plt.show()
 
PREVIOUS NEXT
Tagged: #matplotlib #keyboard #event
ADD COMMENT
Topic
Name
7+3 =