I'm trying to plot a real time line graph with Bokeh. But my code just plots a blank figure.
import numpy as np
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, curdoc
source = ColumnDataSource({'x': [], 'y': []})
def update():
new = {'x':[np.random.rand(1,3)],
'y':[np.random.rand(1,3)]}
source.stream(new)
p = figure(plot_width=800,
plot_height=400,
x_range=[0, 1],
y_range=[0, 1],
x_axis_label = 'x',
y_axis_label = 'y',
)
p.line(source=source, x='x', y='y')
curdoc().add_root(p)
curdoc().add_periodic_callback(update, 100)
Here is an instance of np.random.rand(1,3):
array([[0.60098985, 0.33777435, 0.92713769]])
It is an array of one element which is itself an array of 3 elements, so each element of 'x' and 'y' in your source is an array of 3 elements. Which is why nothing shows up.
You can use numpy.ndarray.flatten
def update():
new = {'x':np.random.rand(1,3).flatten(),
'y':np.random.rand(1,3).flatten()}
source.stream(new)
Or just np.random.rand(3)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.