bqplot:性能问题

bqplot: performance issue

我正在研究 bqploton_drag 功能。我注意到它有点滞后.. 我不确定这是不是

所以我的代码如下

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)

简直就是两点相连 你可以拖动蓝色的 我注意到这条线稍微落后于蓝点。

您不能直接拖动线上的一个点。您的方法是我所知道的唯一方法,因此该线将始终遵循散点。我无法让您的代码运行速度明显加快。

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)

编辑:

其实是可以做到的。使用 js 链接。性状必须是同一类型且length/size。在这种情况下,线和散点标记的 '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)