是否真的可以将数据(回调)从 mpld3 传递到 ipython?
Is it actually possible to pass data (callback) from mpld3 to ipython?
使用 mpld3
创建动画图片有很多惊人的可能性。然而,似乎所有的"moving parts"都是JavaScript的责任。此外,互联网和 Stack Overflow 上有很多请求,人们直接询问这种可能性。
Retrieve Data From Dynamic mpld3 plot in python
Get point information after dragging
How to "dump" points selected with the LinkedBrush plugin for mpld3?
mpld3 ~ Select points and get their coordinates?
其中有参考,但所有答案都是错误的,因为他们建议使用某种警报或字幕。然而,第二个 link 最有趣,因为它建议添加一些 HTML-form 并按下按钮将数据从 "client-javascript" 发送到 "server-python"。还有一个有趣的笔记本
http://nbviewer.jupyter.org/gist/aflaxman/11156203
这被许多人称为灵感来源 - 它将输出配置保存在 .html 文件中。也许这个硬盘交换可以用来进一步处理这个信息python。
更进一步,我发现 IPYwidgets
,其中有大量示例甚至是真正交互客户端-服务器的可能性。本质上,我们可能从基本的滑块和按钮开始,但后来我们看到一些更复杂的包是在此基础上构建的:主要是bqplot
,以及其他一些继承的包。
我想要的-只是在图片上拖放一些点,然后将它们传递给iPython做一些进一步的绘图-非常复杂,绝对不能移动到JavaScript。但似乎尽管 bqplot
的团队已经完成了大量工作,但您只能使用一些 "predefined" 组交互,因此拖放行为再次不包括在内。
当我尝试(不是很深入)进入mpld3的源代码并修改它并可能与ipywidgets合并时,我遇到了很多东西都被弃用了,代码发展得非常快,这不符合网上已有的例子:杨树的大多很老,查询也很老。所以我什么也做不了,因为一团糟,很多例子都因为缺乏向后兼容性而失败。
总结。如果有人能提供一些方法来拖放点并将它们的坐标传递给python,我会很高兴,但我会更有帮助 - 能够以更 "abstract manner" 的方式从 mpld3 传递信息,以便包含其他情况。
你可以用新的 bqplot
做到这一点 Scatter
和 Label
它们都有一个 enable_move
参数,当你设置为 True
他们允许拖动点。此外,当您 drag
时,您可以 observe
更改 x
或 y
值 Scatter
或 Label
并触发 python 函数,这又会生成一个新的情节。
清楚了吗?
问这个问题已经快一年了。好吧,答案与 mpld3
无关,但我不坚持那种特定的技术。用户@Drew建议使用bqplot
,所以我post一个link到一个相关的notebook
https://github.com/bloomberg/bqplot/blob/master/examples/Interactions/Interaction%20Layer.ipynb
来自 bloomberg
。如果你打开它,我建议在右上角找到 link,它将你重定向到带有图片的外部 nbviewer。几乎所有内容都包含在那里,我只是尝试重现一个简约的工作示例。
请注意,为了启动扩展名为 bqplot
的 jupyter notebook
以及一些 ipywidgets
,您可能需要执行某种 "magic"这行得通。您需要熟悉一些 bash 命令,例如 jupyter install nbextension
和 jupyter nbextension enable
。我个人不得不与 bqplot
斗争几个小时才能让它发挥作用。但这显然是一个单独的问题。
让我们尝试启动功能 observe
。测试函数 my_callback(...)
只打印事件。
%matplotlib inline
from bqplot import pyplot as plt
def my_callback(change):
print change
scatt = plt.scatter([1,2,3],[4,5,6],enable_move=True)
scatt.observe(my_callback)
plt.show()
你得到了这样一个不错的情节:
具有拖动点的附加功能。拖动一个点后,您会看到一个打印的更改列表,它是一个 python
结构,每个事件都在单独的行上。
{'owner': , 'new': {u'hovered_point': 1}, 'old': traitlets.Undefined, 'name': '_property_lock', 'type': 'change'}
{'owner': , 'new': 1, 'old': None, 'name': 'hovered_point', 'type': 'change'}
{'owner': , 'new': {}, 'old': {u'hovered_point': 1}, 'name': '_property_lock', 'type': 'change'}
{'owner': , 'new': {u'y': {u'type': u'float', u'values': [4, 4.863453784620906, 6]}, u'x': {u'type': u'float', u'values': [1, 2.016078455307904, 3]}}, 'old': {}, 'name': '_property_lock', 'type': 'change'}
{'owner': , 'new': array([ 4. , 4.86345378, 6. ]), 'old': array([4, 5, 6]), 'name': 'y', 'type': 'change'}
{'owner': , 'new': array([ 1. , 2.01607846, 3. ]), 'old': array([1, 2, 3]), 'name': 'x', 'type': 'change'}
{'owner': , 'new': {}, 'old': {u'y': {u'type': u'float', u'values': [4, 4.863453784620906, 6]}, u'x': {u'type': u'float', u'values': [1, 2.016078455307904, 3]}}, 'name': '_property_lock', 'type': 'change'}
{'owner': , 'new': {u'hovered_point': None}, 'old': {}, 'name': '_property_lock', 'type': 'change'}
{'owner': , 'new': None, 'old': 1, 'name': 'hovered_point', 'type': 'change'}
{'owner': , 'new': {}, 'old': {u'hovered_point': None}, 'name': '_property_lock', 'type': 'change'}
我承认这个结构分解起来有点棘手,但仔细看了一下,我们注意到粗线的'name'
等于'_property_lock'
,然后是'new'
子结构包含字段 u'x'
和 u'y'
,这是 "x" 和 "y".
的 Unicode
然后你可以跟踪这些变化,相应地 运行 一些 python
函数内的代码 my_callback(...)
,你甚至可以在这个图中绘制一些东西,或者创建一个新的,等等. 令人惊讶的是,这在某种程度上起作用了,使用新的 jupyter,你甚至可以 用小部件 保存笔记本,这完全令人兴奋。
这也不是 mpld3
,但这里有一个在 jupyter notebook
中使用 bqplot
的简单示例,其灵感来自 的 Sergey comment/question以及谢尔盖和德鲁的回答。
首先在anaconda环境下安装bqplot
,打开notebook
(... do whatever to make anaconda work for you....)
conda install bqplot
jupyter notebook
然后将这段可调的交互式散点图代码粘贴到第一个块中:
import numpy as np
from __future__ import print_function # So that this notebook becomes both Python 2 and Python 3 compatible
from bqplot import pyplot as plt
# 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)
# Creating a new Figure and setting it's title
plt.figure(title='An adjustable, extractable scatter plot')
# Let's assign the scatter plot to a variable
scatter_plot = plt.scatter(x_data, y_data)
plt.show()
scatter_plot.enable_move = True # make the points movable
然后,在绘图出现后,单击并拖动一两个数据点,然后在下一个块中查看绘图内的变化:
print([x_data-scatter_plot.x,y_data-scatter_plot.y])
我原以为 https://github.com/bloomberg/bqplot/blob/master/examples/Introduction.ipynb 中的回调内容是必需的,但只有当你想在修改时触发一些代码时才需要它。
为此,请尝试以下方法:
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]
#print (pdata)
# Hook up our function `foo` to the coordinates attributes (or Traits) of the scatter plot
scatter_plot.observe(foo, ['y','x'])
然后 x,y
坐标上的变化触发 foo
并改变全局变量 pdata
。您会看到 foo()
的打印输出附加到第一个块的输出,更新后的 pdata 将可用于以后的代码块。
使用 mpld3
创建动画图片有很多惊人的可能性。然而,似乎所有的"moving parts"都是JavaScript的责任。此外,互联网和 Stack Overflow 上有很多请求,人们直接询问这种可能性。
Retrieve Data From Dynamic mpld3 plot in python
Get point information after dragging
How to "dump" points selected with the LinkedBrush plugin for mpld3?
mpld3 ~ Select points and get their coordinates?
其中有参考,但所有答案都是错误的,因为他们建议使用某种警报或字幕。然而,第二个 link 最有趣,因为它建议添加一些 HTML-form 并按下按钮将数据从 "client-javascript" 发送到 "server-python"。还有一个有趣的笔记本
http://nbviewer.jupyter.org/gist/aflaxman/11156203
这被许多人称为灵感来源 - 它将输出配置保存在 .html 文件中。也许这个硬盘交换可以用来进一步处理这个信息python。
更进一步,我发现 IPYwidgets
,其中有大量示例甚至是真正交互客户端-服务器的可能性。本质上,我们可能从基本的滑块和按钮开始,但后来我们看到一些更复杂的包是在此基础上构建的:主要是bqplot
,以及其他一些继承的包。
我想要的-只是在图片上拖放一些点,然后将它们传递给iPython做一些进一步的绘图-非常复杂,绝对不能移动到JavaScript。但似乎尽管 bqplot
的团队已经完成了大量工作,但您只能使用一些 "predefined" 组交互,因此拖放行为再次不包括在内。
当我尝试(不是很深入)进入mpld3的源代码并修改它并可能与ipywidgets合并时,我遇到了很多东西都被弃用了,代码发展得非常快,这不符合网上已有的例子:杨树的大多很老,查询也很老。所以我什么也做不了,因为一团糟,很多例子都因为缺乏向后兼容性而失败。
总结。如果有人能提供一些方法来拖放点并将它们的坐标传递给python,我会很高兴,但我会更有帮助 - 能够以更 "abstract manner" 的方式从 mpld3 传递信息,以便包含其他情况。
你可以用新的 bqplot
做到这一点 Scatter
和 Label
它们都有一个 enable_move
参数,当你设置为 True
他们允许拖动点。此外,当您 drag
时,您可以 observe
更改 x
或 y
值 Scatter
或 Label
并触发 python 函数,这又会生成一个新的情节。
清楚了吗?
问这个问题已经快一年了。好吧,答案与 mpld3
无关,但我不坚持那种特定的技术。用户@Drew建议使用bqplot
,所以我post一个link到一个相关的notebook
https://github.com/bloomberg/bqplot/blob/master/examples/Interactions/Interaction%20Layer.ipynb
来自 bloomberg
。如果你打开它,我建议在右上角找到 link,它将你重定向到带有图片的外部 nbviewer。几乎所有内容都包含在那里,我只是尝试重现一个简约的工作示例。
请注意,为了启动扩展名为 bqplot
的 jupyter notebook
以及一些 ipywidgets
,您可能需要执行某种 "magic"这行得通。您需要熟悉一些 bash 命令,例如 jupyter install nbextension
和 jupyter nbextension enable
。我个人不得不与 bqplot
斗争几个小时才能让它发挥作用。但这显然是一个单独的问题。
让我们尝试启动功能 observe
。测试函数 my_callback(...)
只打印事件。
%matplotlib inline
from bqplot import pyplot as plt
def my_callback(change):
print change
scatt = plt.scatter([1,2,3],[4,5,6],enable_move=True)
scatt.observe(my_callback)
plt.show()
你得到了这样一个不错的情节:
具有拖动点的附加功能。拖动一个点后,您会看到一个打印的更改列表,它是一个 python
结构,每个事件都在单独的行上。
{'owner': , 'new': {u'hovered_point': 1}, 'old': traitlets.Undefined, 'name': '_property_lock', 'type': 'change'}
{'owner': , 'new': 1, 'old': None, 'name': 'hovered_point', 'type': 'change'}
{'owner': , 'new': {}, 'old': {u'hovered_point': 1}, 'name': '_property_lock', 'type': 'change'}
{'owner': , 'new': {u'y': {u'type': u'float', u'values': [4, 4.863453784620906, 6]}, u'x': {u'type': u'float', u'values': [1, 2.016078455307904, 3]}}, 'old': {}, 'name': '_property_lock', 'type': 'change'}
{'owner': , 'new': array([ 4. , 4.86345378, 6. ]), 'old': array([4, 5, 6]), 'name': 'y', 'type': 'change'}
{'owner': , 'new': array([ 1. , 2.01607846, 3. ]), 'old': array([1, 2, 3]), 'name': 'x', 'type': 'change'}
{'owner': , 'new': {}, 'old': {u'y': {u'type': u'float', u'values': [4, 4.863453784620906, 6]}, u'x': {u'type': u'float', u'values': [1, 2.016078455307904, 3]}}, 'name': '_property_lock', 'type': 'change'}
{'owner': , 'new': {u'hovered_point': None}, 'old': {}, 'name': '_property_lock', 'type': 'change'}
{'owner': , 'new': None, 'old': 1, 'name': 'hovered_point', 'type': 'change'}
{'owner': , 'new': {}, 'old': {u'hovered_point': None}, 'name': '_property_lock', 'type': 'change'}
我承认这个结构分解起来有点棘手,但仔细看了一下,我们注意到粗线的'name'
等于'_property_lock'
,然后是'new'
子结构包含字段 u'x'
和 u'y'
,这是 "x" 和 "y".
然后你可以跟踪这些变化,相应地 运行 一些 python
函数内的代码 my_callback(...)
,你甚至可以在这个图中绘制一些东西,或者创建一个新的,等等. 令人惊讶的是,这在某种程度上起作用了,使用新的 jupyter,你甚至可以 用小部件 保存笔记本,这完全令人兴奋。
这也不是 mpld3
,但这里有一个在 jupyter notebook
中使用 bqplot
的简单示例,其灵感来自
首先在anaconda环境下安装bqplot
,打开notebook
(... do whatever to make anaconda work for you....)
conda install bqplot
jupyter notebook
然后将这段可调的交互式散点图代码粘贴到第一个块中:
import numpy as np
from __future__ import print_function # So that this notebook becomes both Python 2 and Python 3 compatible
from bqplot import pyplot as plt
# 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)
# Creating a new Figure and setting it's title
plt.figure(title='An adjustable, extractable scatter plot')
# Let's assign the scatter plot to a variable
scatter_plot = plt.scatter(x_data, y_data)
plt.show()
scatter_plot.enable_move = True # make the points movable
然后,在绘图出现后,单击并拖动一两个数据点,然后在下一个块中查看绘图内的变化:
print([x_data-scatter_plot.x,y_data-scatter_plot.y])
我原以为 https://github.com/bloomberg/bqplot/blob/master/examples/Introduction.ipynb 中的回调内容是必需的,但只有当你想在修改时触发一些代码时才需要它。
为此,请尝试以下方法:
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]
#print (pdata)
# Hook up our function `foo` to the coordinates attributes (or Traits) of the scatter plot
scatter_plot.observe(foo, ['y','x'])
然后 x,y
坐标上的变化触发 foo
并改变全局变量 pdata
。您会看到 foo()
的打印输出附加到第一个块的输出,更新后的 pdata 将可用于以后的代码块。