擅长:python、mysql、java
<p>要扩展ImportanceOfBeingErnest的答案,可以使用<a href="https://matplotlib.org/users/event_handling.html" rel="nofollow noreferrer">^{<cd1>}</a>在单击时提供回调,使用<a href="https://ipywidgets.readthedocs.io/en/stable/index.html" rel="nofollow noreferrer">^{<cd2>}</a>显示回调的输出。如果需要,可以在不同的单元格中分解代码。</p>
<pre><code>%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
import ipywidgets as wdg # Using the ipython notebook widgets
# Create a random image
a = np.random.poisson(size=(12,15))
fig = plt.figure()
plt.imshow(a)
# Create and display textarea widget
txt = wdg.Textarea(
value='',
placeholder='',
description='event:',
disabled=False
)
display(txt)
# Define a callback function that will update the textarea
def onclick(event):
txt.value = str(event) # Dynamically update the text box above
# Create an hard reference to the callback not to be cleared by the garbage collector
ka = fig.canvas.mpl_connect('button_press_event', onclick)
</code></pre>