我试图用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)
我想知道这是不是一个bug。版本: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)
当使用分类(即字符串)坐标时,您必须通知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)
这导致了这个输出: