有没有办法在Bokeh中使用基于文本的X值?

Ist*_*van 5 python bokeh

我试图用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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想知道这是不是一个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)
Run Code Online (Sandbox Code Playgroud)

big*_*dot 2

当使用分类(即字符串)坐标时,您必须告知 Bokeh 分类因素的顺序应该是什么。它是任意的,并且由您决定,Bokeh 默认情况下无法选择顺序。对于简单的非嵌套类别,最容易通过将列表作为figure参数传递来完成x_range

所有这些信息都在文档中:处理分类数据

您的代码已更新:

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)
Run Code Online (Sandbox Code Playgroud)

这会产生以下输出:

在此输入图像描述