如何使用散景为圆圈设置动画

时间:2015-06-23 16:46:09

标签: python-2.7 bokeh

我目前正在使用散景图通过服务器进行这个简单的项目,尝试在圆圈中移动圆圈。我一直试图学习的两个例子是https://github.com/bokeh/bokeh/blob/master/examples/plotting/server/animated.pyhttps://github.com/bokeh/bokeh/blob/master/examples/plotting/server/line_animate.py

由于他们的文件仍然非常有限,如果有人可以提供帮助,那就太棒了。

import time 
import numpy as np
from bokeh.plotting import cursession, figure, show, output_server


output_server("circle_server")

pl = figure(y_range=(-2,2), x_range=(-2,2))

x=1
y=0

pl.circle(x, y, size=25, alpha=0.6, name="moving_circle") 
pl.annulus(x=0,y=0, inner_radius = 1, outer_radius = 1, line_alpha=0.6)


show(pl)

renderer = pl.select(dict(name="moving_circle"))
ds = renderer[0].data_source



while True:
    for rad in np.linspace(0,2*np.pi,100):
        #rad = deg*np.pi/180
        ds.data["x"] = np.cos(rad)
        ds.data["y"] = np.sin(rad)
        cursession().store_objects(ds)
        time.sleep(0.1)

2 个答案:

答案 0 :(得分:2)

以后参考cursession已过时,应按照here的标准迁移到bokeh.session

可以找到带有动画的(环形楔形)示例here

# You must first run "bokeh serve" to view this example

from numpy import pi, cos, sin, linspace, roll

from bokeh.client import push_session
from bokeh.io import curdoc
from bokeh.plotting import figure

M = 5
N = M*10 + 1
r_base = 8
theta = linspace(0, 2*pi, N)
r_x = linspace(0, 6*pi, N-1)
rmin = r_base - cos(r_x) - 1
rmax = r_base + sin(r_x) + 1

colors = ["FFFFCC", "#C7E9B4", "#7FCDBB", "#41B6C4", "#2C7FB8", "#253494", "#2C7FB8", "#41B6C4", "#7FCDBB", "#C7E9B4"] * M

# figure() function auto-adds the figure to curdoc()
p = figure(x_range=(-11, 11), y_range=(-11, 11))
r = p.annular_wedge(0, 0, rmin, rmax, theta[:-1], theta[1:],
                fill_color=colors, line_color="white")

# open a session to keep our local document in sync with server
session = push_session(curdoc())

ds = r.data_source

def update():
    rmin = roll(ds.data["inner_radius"], 1)
    rmax = roll(ds.data["outer_radius"], -1)
    ds.data.update(inner_radius=rmin, outer_radius=rmax)

curdoc().add_periodic_callback(update, 30)

session.show(p) # open the document in a browser

session.loop_until_closed() # run forever

答案 1 :(得分:1)

来自这里的文档:

http://bokeh.pydata.org/en/latest/docs/reference/plotting.html#bokeh.plotting.Figure.circle

circle(plot,* args,** kwargs) - 参数:

  • x(str或list [float]) - 中心x坐标的值或字段名称

  • y(str或list [float]) - 中心y坐标的值或字段名称

因此,您需要在列表中传递值。要修复它,你只需在[x],[y],[np.cos(rad)]和[np.sin(rad)]中添加括号。

这是一个经过测试的工作解决方案:

import time 
import numpy as np
from bokeh.plotting import cursession, figure, show, output_server


output_server("circle_server")

pl = figure(y_range=(-2,2), x_range=(-2,2))

x=1
y=0


pl.circle(x=[x], y=[y], size=25, alpha=0.6, name="circle") 
pl.annulus(x=0,y=0, inner_radius = 1, outer_radius = 1, line_alpha=0.6)


show(pl)

renderer = pl.select(dict(name="circle"))

ds = renderer[0].data_source

while True:
    for rad in np.linspace(0,2*np.pi,100):
        #rad = deg*np.pi/180
        ds.data["x"] = [np.cos(rad)]
        ds.data["y"] = [np.sin(rad)]
        cursession().store_objects(ds)
        time.sleep(0.5)