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

标签 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)

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)

最佳答案

当使用分类(即字符串)坐标时,您必须通知 Bokeh 分类因子的顺序应该是什么。它是任意的,并且由您决定,Bokeh 没有默认选择的顺序。对于简单的非嵌套类别,最简单的方法是将列表作为 x_range 参数传递给 figure

所有这些信息都在文档中: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

关于python - 有没有办法在 Bokeh 中使用基于文本的 X 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52189407/

相关文章:

python - 检测是用python或python3运行的python程序

ipython - 将绘图拟合到绘图窗口中

Python - 使用 Holoviews Bokeh 绘制大型数据集的特定子集

python - 如何在 Django 或 Flask 中嵌入交互式 Bokeh 或 Dash 应用*带身份验证*?

python - 如何在 Bokeh 中仅绘制一 strip ?

python - 将 tqdm 进度条与 asyncio 结合使用

python - z3opt python——最小化平方

python - Bokeh 概念验证有效的动态图更新?

python - 计算 Pandas 地理密度的有效方法?

python - 如何将光照应用于 pyopengl 上的 .obj 文件