Bokeh的交互式热图

2024-09-27 19:21:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个dataframe的坐标和时间戳,我试图将它们形象化为heatmap,在这里我可以使用一个滑动条来调整时间,并且只选择与这个特定时间相关联的点。 为此,我使用Bokeh创建一个小部件并输入variable H,定义小时(0到23之间)。由于我在Bokeh中找不到任何现成的工具,heatmaps我构建了一个二维直方图并将其绘制为Bokeh图像。在

但不幸的是,滑块在玩的时候没有更新数据。。。你知道为什么它不起作用吗?在

x=itineraries['Itinerary Longitude Point A']
y=itineraries['Itinerary Latitude Point A']
a=itineraries['Itinerary Longitude Point A']
b=itineraries['Itinerary Latitude Point A']
z=itineraries['Itinerary Created Hour of Day']

source = ColumnDataSource(data=dict(x=x, y=y,z=z,a=a, b=b, p=p))
original_source = ColumnDataSource(data=dict(x=a, y=b,z=z))

plot = figure(y_range=(-24, -23), x_range=(-47, -46))

C, xedges, yedges = np.histogram2d(source.to_df()['y'], source.to_df()['x'], bins=75, range=[[-24,-23], [-47,-46]], normed = True)
X, Y = np.meshgrid(xedges, yedges)

plot.image(image = [C], x=-47, y=-24, dw=1, dh=1, 
           palette=['#f7fbff',
                    '#deebf7',
                    '#c6dbef',
                    '#9ecae1',
                    '#6baed6',
                    '#4292c6',
                    '#2171b5',
                    '#FFFFFF'])


callback = CustomJS(args=dict(source=source), code="""
    var data = source.data;
    var H = cb_obj.value;

    x = data['x']
    y = data['y']
    z = data['z']
    a = data['a']
    b = data['b']

    for (i = 0; i < x.length; i++) 
    {
        if (z[i]==H) 
        {
            x[i]=a[i]
            y[i]=b[i]
        }
        else
        {
            x[i]=-23
            y[i]=-47
        }

    }

    source.change.emit();
""")

hour_slider = Slider(start=0, end=23, value=12, step=1, title="Hour of day")
hour_slider.js_on_change('value', callback)

layout = row(
    plot,
    widgetbox(hour_slider),
)

output_file("slider.html", title="slider.py example")

show(layout)

欢迎任何提示!在


Tags: sourcedataplotvalue时间bokehrangedict

热门问题