代码之家  ›  专栏  ›  技术社区  ›  Istvan

有没有一种方法可以在Bokeh中使用基于文本的X值?

  •  5
  • Istvan  · 技术社区  · 7 年前

    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)
    

    enter image description here

    我想知道这是不是个虫子。版本: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)
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   bigreddot    7 年前

    当使用分类(即字符串)坐标时,您必须告知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)
    

    结果是:

    enter image description here