Я пытаюсь создать интерактивную диаграмму рассеяния с помощью matplotlib в блокноте jupyter.Теперь для каждой выбранной точки я хотел бы показать некоторую дополнительную информацию (не в тексте, а на самом деле некоторые 3D-графики, которые могут быть сделаны только с помощью HTML + javascript).
Поскольку я новичок в mpl, я начну с очень простого примера (как показано ниже).В моем воображении я мог либо отобразить (HTML (".....")), либо вызвать некоторую функцию для запуска другой ячейки (путем вызова run_cell_by_tag (tag)) в блокноте jupyter, который выполняет 3D-графики.Тем не менее, в моих попытках ни одна из них не сработала ...
Любое предложение будет действительно оценено, спасибо заранее!
%%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()
Это то, что я ожидал