散景流动空白图

时间:2018-03-27 09:24:50

标签: python plot tornado bokeh

我正在尝试使用Bokeh绘制实时线图。但我的代码只是绘制了一个空白数字。

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)

1 个答案:

答案 0 :(得分:0)

这是np.random.rand(1,3)的一个实例:

array([[0.60098985, 0.33777435, 0.92713769]])

它是一个元素的数组,它本身就是一个包含3个元素的数组,因此源中'x'和'y'的每个元素都是一个包含3个元素的数组。这就是为什么没有出现的原因。

您可以使用numpy.ndarray.flatten

def update():
    new = {'x':np.random.rand(1,3).flatten(),
           'y':np.random.rand(1,3).flatten()}
    source.stream(new)

或只是np.random.rand(3)