bqplot:性能问题

问题描述 投票:1回答:1

我正在玩on_dragbqplot功能。而且我注意到它有点滞后......我不确定这是不是

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

所以我的代码如下

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)

它只是两个点连接,你可以拖动蓝色的一个我注意到的线条有点落后于蓝点。

python ipython jupyter-notebook jupyter bqplot
1个回答
0
投票

您无法直接在一条线上拖动一个点。您的方法只是我知道的方法,因此该行将始终遵循散点。我无法让你的代码明显加快。

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)

Edit:

实际上可以做到。使用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)
© www.soinside.com 2019 - 2024. All rights reserved.