我正在尝试使用Bokeh绘制一个简单的图表,但是当x值基于文本时,它无法显示任何内容:
x=['-', 'AF', 'AS', 'EU', 'NA', 'OC', 'SA']
y=[8, 7621750, 33785311, 31486697, 38006434, 7312002, 7284879]
p = figure(plot_width=480, plot_height=300,title='test')
p.vbar(x=x, width=0.5, bottom=0, top=y, color="navy", alpha=0.5)
p.toolbar.logo = None
p.toolbar_location = None
v = gridplot([[p]])
show(v)
我想知道这是否是错误。版本:0.13.0
应用建议的修复程序后,它会起作用:
for i in range(4):
ind=i+offset
rez[ind].sort(key=lambda tup: tup[0])
x = [x[0] for x in rez[ind]]
y = [x[1] for x in rez[ind]]
if type(x[0]) == str:
charts[i] = figure(
plot_width=480,
plot_height=300,
title=columns_being_investigated[ind],
x_range=x)
else:
charts[i] = figure(
plot_width=480,
plot_height=300,
title=columns_being_investigated[ind])
charts[i].vbar(x=x, width=0.5, bottom=0, top=y, color="navy", alpha=0.5)
charts[i].toolbar.logo = None
charts[i].toolbar_location = None
p = gridplot([[charts[0], charts[1]], [charts[2], charts[3]]])
show(p)
答案 0 :(得分:2)
使用分类(即字符串)坐标时,您必须告知Bokeh分类因子的顺序。这是任意的,由您决定,Bokeh默认情况下无法选择。对于简单的非嵌套类别,最简单的方法是将列表作为figure
参数传递到x_range
。
所有这些信息都在文档中:Handling Categorical Data
您的代码已更新:
from bokeh.plotting import figure, show
x=['-', 'AF', 'AS', 'EU', 'NA', 'OC', 'SA']
y=[8, 7621750, 33785311, 31486697, 38006434, 7312002, 7284879]
p = figure(plot_width=480, plot_height=300,title='test',
# you were missing this:
x_range=['-', 'AF', 'AS', 'EU', 'NA', 'OC', 'SA'])
p.vbar(x=x, width=0.5, bottom=0, top=y, color="navy", alpha=0.5)
p.toolbar.logo = None
p.toolbar_location = None
show(p)
结果如下: