%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)
要扩展ImportanceOfBeingErnest的答案,可以使用^{} 在单击时提供回调,使用^{} 显示回调的输出。如果需要,可以在不同的单元格中分解代码。
后端
%matpotlib inline
将打印输出显示为png图像。也许可以为Jupyter笔记本编写一些JavaScript,以便在单元格输出中的图像上通过鼠标获得颜色和像素。不过,只使用
%matplotlib notebook
后端可能要容易得多,这样在将matplotlib图形打印到输出时,它会保持其活动状态,因此通常内置的mouseover功能很容易获得。注意图像右下角的选择器,它显示
x
、y
和当前像素的值。相关问题 更多 >
编程相关推荐