1

我正在on_drag玩弄bqplot. 我注意到它有点滞后..我不确定这是否是

  • 真正的问题
  • 我做了不恰当的事
  • 意味着像它一样工作

所以我的代码如下

from bqplot import pyplot as plt
import numpy as np
fig=plt.figure()
lin=plt.plot([0,1],
             [0,0])
scatt1=plt.scatter([0],[0],colors=['Red'])
scatt2=plt.scatter([1],[0],enable_move=True)

plt.xlim(-3,3)
plt.ylim(-3,3)
fig.layout.height = '500px'
fig.layout.width = '500px'
plt.show()
def call_back2(name, value):
   #print(value,name)
   if value['point']:
       X=value['point']['x']
       Y=value['point']['y']
       lin.x=[scatt1.x[0],(X-scatt1.x)[0]]
       lin.y=[scatt1.y[0],(Y-scatt1.y)[0]]

scatt2.on_drag_start(call_back2)
scatt2.on_drag(call_back2)
scatt2.on_drag_end(call_back2)

它只是连接了两个点,您可以将蓝色的点拖动到周围,我注意到的是,这条线略微落后于蓝点。

4

1 回答 1

1

您不能直接在一条线上拖动一个点。您的方法是我所知道的唯一方法,因此该线将始终跟随散点。我无法让您的代码明显更快。

from bqplot import pyplot as plt
import numpy as np
fig = plt.figure()
lin=plt.plot([0,1],
             [0,0])
scatt1=plt.scatter([0],[0],colors=['Red'])
scatt2=plt.scatter([1],[0],enable_move=True)
# scatt2.update_on_move = True

plt.xlim(-3,3)
plt.ylim(-3,3)
fig.layout.height = '500px'
fig.layout.width = '500px'
plt.show()
def call_back2(name, value):
#     with lin.hold_sync():
   lin.x=[lin.x[0], value['point']['x']]
   lin.y=[lin.y[0], value['point']['y']]    

# scatt2.on_drag_start(call_back2)
scatt2.on_drag(call_back2 )
# scatt2.on_drag_end(call_back2)

编辑:

其实是可以做到的。使用 jslink。特征必须是相同的类型和长度/大小。在这种情况下,线和散点标记的 'x' 和 'y' 特征是长度为 2 的数组。

from bqplot import pyplot as plt
from ipywidgets import jslink

fig = plt.figure()

# Strange bug when 1st point is 0,0. Red point flickers.  
lin=plt.plot([0.0001,1],
             [0.0001,0])   

scatt2=plt.scatter(lin.x, lin.y, enable_move = True, colors = ['Red','blue'])
scatt2.update_on_move = True

# Cover up 1st point so it can't be moved. 
# Just remove this line if you want both points to be moveable
scatt3=plt.scatter([lin.x[0]], [lin.y[0]], colors = ['Red']) 


plt.xlim(-3,3)
plt.ylim(-3,3)
fig.layout.height = '500px'
fig.layout.width = '500px'
plt.show()

jslink((scatt2, 'x'), (lin, 'x'))
jslink((scatt2, 'y'), (lin, 'y'))

Jslink 不需要 Python 内核来实现交互性。您可以通过以下方式创建一个 html 文件(和 js 目录):

import ipyvolume.embed
ipyvolume.embed.embed_html("bqplot.html", fig, offline=True, devmode=False)
于 2017-11-14T14:31:46.577 回答