我正在通过查询数据库中的数据然后使用CustomJSFilter对其进行过滤来绘制条形图。代码执行但绘图除了轴标签外是空的。
如果我修改SQL查询以过滤到JS过滤器中的同一天,则该图将返回数据。
我在下面的代码中使用示例数据复制了错误。如果你运行它,你会看到没有绘制任何值。
如果数据替换为:
area = ['Chester','Liverpool', 'Birmingham']
date = ['2017-09-22','2017-09-22','2017-09-22']
roomsavailable = [10,20,15]
即。只包括一个日期然后情节工作。
这告诉我CDSFilter或CustomJSFilter存在问题。它在有重复数据时无法工作,但在删除重复时有效。
这是Bokeh的错误还是我的代码有问题?
import sqlite3
import pandas as pd
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, Label, LabelSet,CDSView, CustomJSFilter, Filter, IndexFilter
from bokeh.layouts import row
from bokeh.plotting import figure
from bokeh.palettes import Spectral9, Viridis256
area = ['Chester','Liverpool', 'Birmingham','Chester','Liverpool', 'Birmingham']
date = ['2017-09-01','2017-09-01','2017-09-01','2017-09-22','2017-09-22','2017-09-22']
roomsavailable = [10,20,15,12,14,18]
source = ColumnDataSource(
data=dict(area=area, roomsavailable=roomsavailable, date=date))
max_room = int(20 * round(float(max(roomsavailable)) / 20))
custom_filter = CustomJSFilter(code = '''
var indices = [];
for (var i = 0; i <= source.data['date'].length; i++){
if (source.data['date'][i] == '2017-09-22') {
indices.push(i)
}
}
return indices;
''')
view = CDSView(source=source, filters=[custom_filter])
f_area = list(set(area)) #Get a distinct list of areas for plotting
p = figure(y_range=f_area, x_range=(0, 30), plot_width=600, plot_height=550, toolbar_location=None,
title="Chester Student Rooms Available as of")
p.hbar(y='area', left=0, right='roomsavailable', height=0.8,source=source,view=view)
p.ygrid.grid_line_color = None
p.xaxis.axis_label = "Available Rooms"
p.outline_line_color = None
show(row(p))