Search
 
SCRIPT & CODE EXAMPLE
 

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

PREVIOUS NEXT
Code Example
Python :: InsertionSort 
Python :: python append many items to a list 
Python :: tadjust margines automatically matplotlib 
Python :: create tab in python text 
Python :: How to append variable in Python 
Python :: Python OrderedDict - LRU 
Python :: how do you make plot show with matplotlib ion method 
Python :: keras.utils.plot_model keeps telling me to install pydot and graphviz 
Python :: doormat pattern python 
Python :: boto3 upload to digital digitalocean folder 
Python :: pandas math operation from string 
Python :: print A to Z in python uppercase 
Python :: print(s[::-1]) 
Python :: summation 
Python :: does pygame work on python 3.10.1 
Python :: showing typle results with for loop in py 
Python :: django Mixed Content: The page at ' was loaded over HTTPS, but requested an insecure resource swagger 
Python :: loosen_pickle 
Python :: point at the middle of a dataframe 
Python :: pyglet template 
Python :: capitalise.texts 
Python :: IndexError: child index out of range in parsing xml for object detection 
Python :: custom Yolo object detection python 
Python :: set colour to inserplaintext qtextedit in python 
Python :: add a third dimension matrix dataset python 
Python :: Ordering column names sensibly in pandas 
Python :: paramhans ramchandra das 
Python :: recursively count string 
Python :: equivalent of case_when in r in pandas 
Python :: python code to print fibonacci series 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =