问题
Note from maintainers: This question concerns the obsolete first generation Bokeh server. For details about modern Bokeh server applications, see:
https://docs.bokeh.org/en/latest/docs/user_guide/server.html
OBSOLETE:
I am currently working on this simple project using bokeh plotting through a server, attempting to move a circle in a circle. The two examples that I have been trying to learn from are https://github.com/bokeh/bokeh/blob/master/examples/plotting/server/animated.py and https://github.com/bokeh/bokeh/blob/master/examples/plotting/server/line_animate.py
As their documentation is still very limited, if anyone could help, that would be great.
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)
回答1:
Note from maintainers: This question concerns the obsolete first generation Bokeh server. For details about modern Bokeh server applications, see:
https://docs.bokeh.org/en/latest/docs/user_guide/server.html
OBSOLETE:
From the docs here:
http://docs.bokeh.org/en/latest/docs/reference/plotting.html#bokeh.plotting.Figure.circle
circle(plot, *args, **kwargs) - Parameters:
x (str or list[float]) – values or field names of center x coordinates
y (str or list[float]) – values or field names of center y coordinates
So, you need to pass your values in a list. To fix it you can just add brackets in [x], [y], [np.cos(rad)] and [np.sin(rad)].
Here's a tested working solution:
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)
回答2:
Note from maintainers: This question concerns the obsolete first generation Bokeh server. For details about modern Bokeh server applications, see:
https://docs.bokeh.org/en/latest/docs/user_guide/server.html
OBSOLETE:
For future reference cursession
is outdated and one should migrate to bokeh.session
as discussed here.
An (annular wedge) example with animation can be found 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
来源:https://stackoverflow.com/questions/31008714/how-to-animate-a-circle-using-bokeh