如何使用散景制作圆圈动画

How to animate a circle using bokeh

维护者注意:此问题涉及已过时的第一代 Bokeh 服务器。有关现代 Bokeh 服务器应用程序的详细信息,请参阅:

https://docs.bokeh.org/en/latest/docs/user_guide/server.html



已过时:

我目前正在使用通过服务器进行散景绘图的这个简单项目,试图在一个圆圈中移动一个圆圈。我一直在努力学习的两个例子是 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)

维护者注意:这个问题涉及过时的第一代 Bokeh 服务器。有关现代 Bokeh 服务器应用程序的详细信息,请参阅:

https://docs.bokeh.org/en/latest/docs/user_guide/server.html



已过时:

来自此处的文档:

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

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

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

  • y (str or 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)

维护者注意:此问题涉及已过时的第一代 Bokeh 服务器。有关现代 Bokeh 服务器应用程序的详细信息,请参阅:

https://docs.bokeh.org/en/latest/docs/user_guide/server.html



已过时:

供将来参考 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