我正在尝试在jupyter笔记本中使用matplotlib制作交互式散点图。现在,对于每个选取的点,我想显示一些其他信息(不是文本,而是一些只能由HTML + javascript完成的3D图)。
由于我是mpl的新手,所以我从一个非常简单的示例开始(如下所示)。在我的想象中,我可以显示(HTML(“ .....”)),也可以调用一些函数来运行Jupyter笔记本中另一个用于3D绘图的单元格(通过调用run_cell_by_tag(tag))。但是,在我的尝试中,都无法正常工作...
任何建议将不胜感激,在此先感谢!
%%javascript
window.findCellIndicesByTag = function findCellIndicesByTag(tagName) {
return (Jupyter.notebook.get_cells()
.filter(
({metadata: {tags}}) => tags && tags.includes(tagName)
)
.map((cell) => Jupyter.notebook.find_cell_index(cell))
);
};
window.runCells = function runCellsByTag(tags) {
var c = window.findCellIndicesByTag(tags);
Jupyter.notebook.execute_cells(c);
};
from IPython.core.display import Javascript
from IPython.display import display
def run_cell_by_tag(tags):
display(Javascript("window.runCells('"+tags+"')"))
import matplotlib.pyplot as plt
%matplotlib notebook
import numpy as np
fig = plt.figure('Test')
ax = fig.add_subplot(111)
true = [1,2,3,4]
pred = [1.1, 2.5, 3.1, 3.9]
line, = ax.plot(true, pred, 'o', picker=5, color='red')
picked, = ax.plot([],[], 'o', color='yellow')
text = ax.text(0,0,"")
selected_ind = []
def onpick(event):
thispoint = event.artist
xdata = thispoint.get_xdata()
ydata = thispoint.get_ydata()
ind = event.ind
selected_ind.append(ind[0])
text.set_position((xdata[ind], ydata[ind]))
text.set_text(str(ind[0]) +":true="+ str(xdata[ind[0]])+",pred=" + str(ydata[ind[0]])[:3])
picked.set_data(xdata[ind[0]], ydata[ind[0]])
##Is it possible to call "run_cell_by_tag", or display(HTML("")) here??
##I need to pass over "ind" to make the 3D plot<---------------
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()