Jupyter(IPython)笔记本中的交互式绘图,带有可拖动的点,在拖动时调用Python代码

Dav*_*son 16 javascript python matplotlib ipython-notebook

我想在Jupyter笔记本中制作一些交互式图,其中图中的某些点可以被用户拖动.然后,这些点的位置应该用作更新绘图的Python函数(在笔记本中)的输入.

这样的事情已在这里完成:

http://nbviewer.ipython.org/github/maojrs/ipynotebooks/blob/master/interactive_test.ipynb

但回调是Javascript函数.在某些情况下,更新绘图的代码需要非常复杂,并且需要很长时间才能在Javascript中重写.如果有必要,我愿意在Javascript中指定可拖动点,但是可以回调Python来更新情节吗?

我想知道像Bokeh或Plotly这样的工具是否可以提供此功能.

Dre*_*rew 5

您尝试过bqplot吗?有Scatter一个enable_move参数,当您设置该参数时,True它们允许拖动点。x此外,当您拖动时,您可以观察或 的值y的变化,并通过它触发一个 python 函数,从而生成一个新的绘图。他们在简介笔记本中这样做。ScatterLabel

Jupyter笔记本代码:

# Let's begin by importing some libraries we'll need
import numpy as np
from __future__ import print_function # So that this notebook becomes both Python 2 and Python 3 compatible

# And creating some random data
size = 10
np.random.seed(0)
x_data = np.arange(size)
y_data = np.cumsum(np.random.randn(size)  * 100.0)

from bqplot import pyplot as plt

# Creating a new Figure and setting it's title
plt.figure(title='My Second Chart')
# Let's assign the scatter plot to a variable
scatter_plot = plt.scatter(x_data, y_data)

# Let's show the plot
plt.show()

# then enable modification and attach a callback function:

def foo(change):
    print('This is a trait change. Foo was called by the fact that we moved the Scatter')
    print('In fact, the Scatter plot sent us all the new data: ')
    print('To access the data, try modifying the function and printing the data variable')
    global pdata 
    pdata = [scatter_plot.x,scatter_plot.y]

# First, we hook up our function `foo` to the colors attribute (or Trait) of the scatter plot
scatter_plot.observe(foo, ['y','x'])

scatter_plot.enable_move = True
Run Code Online (Sandbox Code Playgroud)