调试Jupyter笔记本中的交互式matplotlib图形

调试Jupyter笔记本中的交互式matplotlib图形,matplotlib,jupyter-notebook,interactive,Matplotlib,Jupyter Notebook,Interactive,下面的示例应突出显示您单击的点,并更改图形标题以显示与该点关联的标签 如果我以脚本的形式运行这个Python脚本,当我点击一个点时,我会在onpick中得到一个错误“第15行” TypeError:只能将整数标量数组转换为标量索引”,这是预期的event.ind是一个列表,我需要将其更改为ind=event.ind[0],以便在此处更正 但是,当我在Jupyter笔记本中运行此程序时,会出现该图,但错误会被默默忽略,因此代码似乎不起作用。有没有办法让Jupyter告诉我发生了错误 import

下面的示例应突出显示您单击的点,并更改图形标题以显示与该点关联的标签

如果我以脚本的形式运行这个Python脚本,当我点击一个点时,我会在onpick中得到一个错误“第15行” TypeError:只能将整数标量数组转换为标量索引”,这是预期的
event.ind
是一个列表,我需要将其更改为
ind=event.ind[0]
,以便在此处更正

但是,当我在Jupyter笔记本中运行此程序时,会出现该图,但错误会被默默忽略,因此代码似乎不起作用。有没有办法让Jupyter告诉我发生了错误

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = [0, 1, 2, 3, 4, 5]
labels = ['a', 'b', 'c', 'd', 'e', 'f']
ax.plot(x, 'bo', picker=5)

# this is the transparent marker for the selected data point
marker, = ax.plot([0], [0], 'yo', visible=False, alpha=0.8, ms=15)

def onpick(event):
    ind = event.ind
    ax.set_title('Data point {0} is labeled "{1}"'.format(ind, labels[ind]))
    marker.set_visible(True)
    marker.set_xdata(x[ind])
    marker.set_ydata(x[ind])

    ax.figure.canvas.draw()  # this line is critical to change the linewidth

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()
如果您省略matplotlib(可能会发现一个仅在内置模块中发生错误的示例,例如,在运行tkinter应用程序时创建错误),并使用
python
标记对此进行标记,可能会引起更多注意。如果您省略matplotlib(也许可以找到一个仅在内置模块中发生错误的示例,例如,在运行tkinter应用程序时创建错误),并使用
python
标记对此进行标记,这可能会引起更多注意。