问题描述
当我运行带有事件的交互式绘图时,事件处理期间发生的任何异常都会被jupyter静默。在此示例代码中,每次鼠标悬停绘图时都会引发TypeError。但是jupyter笔记本电脑不会在任何地方显示它。我该如何解决?
顺便说一句,当我将代码作为独立脚本运行时,一切都会按预期进行。
- python:2.7.16
- matplotlib:2.2.3
- jupyter:1.0.0
- 笔记本:5.7.8
谢谢!
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
x = np.random.rand(15)
y = np.random.rand(15)
names = np.array(list("ABCDEFGHIJKLMNO"))
c = np.random.randint(1,5,size=15)
norm = plt.Normalize(1,4)
cmap = plt.cm.RdYlGn
fig,ax = plt.subplots()
sc = plt.scatter(x,y,c=c,s=100,cmap=cmap,norm=norm)
annot = ax.annotate("",xy=(0,0),xytext=(20,20),textcoords="offset points",bbox=dict(boxstyle="round",fc="w"),arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)
def update_annot(ind):
pos = sc.get_offsets()[ind["ind"][0]]
annot.xy = pos
text = "{},{}".format(" ".join(list(map(str,ind["ind"])))," ".join([names[n] for n in ind["ind"]]))
annot.set_text(text)
annot.get_bbox_patch().set_facecolor(cmap(norm(c[ind["ind"][0]])))
annot.get_bbox_patch().set_alpha(0.4)
def hover(event):
vis = annot.get_visible()
if event.inaxes == ax:
cont,ind = sc.contains(event)
if cont:
update_annot(ind)
annot.set_visible(True)
fig.canvas.draw_idle()
else:
if vis:
annot.set_visible(False)
fig.canvas.draw_idle()
raise TypeError("This is silenced!")
fig.canvas.mpl_connect("motion_notify_event",hover)
plt.show()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)