使用Bokeh的下拉菜单,这将创建不同的图表

2024-04-16 04:11:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我的工作创建一个下拉界面。我的数据集是这样的,它是一个随机的数据集

enter image description here

现在我想要两个下拉列表说CNN和BBC在这里。在从下拉列表中选择一个频道后,我想选择一个主题,它将根据其值生成条形图。在

我最初只尝试访问一个值,但它给了我一个空白图。在

from bokeh.plotting import figure

from bokeh.io import output_notebook,show,output_file

p=figure()

import csv
data = [row for row in csv.reader(open('C:/Users/Aishwarya/Documents/books/books_q4/crowd_computing/Bokeh-Python-Visualization-master/interactive/data/data.csv', 'r',encoding="utf8"))]

p.vbar(x=data[1][2], width=0.5, bottom=0,
            top=data[1][1], color="firebrick")

#output_notebook()
output_file('1.html')

show(p)

Tags: csv数据fromimport列表outputdata界面
1条回答
网友
1楼 · 发布于 2024-04-16 04:11:17

可能存在两个问题:

  • 第一个是,如果你在一个轴上使用分类坐标,例如“CNN”,你需要etll Bokeh什么是分类范围:

    p.figure(x_range=["CNN", ...]) # list all the factors for x_range
    

    如果以后需要更新轴,可以直接更新范围:

    p.x_range.factors = [...]
    
  • 另外,在Bokeh 0.13.0中,有一个当前尚未解决的问题,它阻止“单一”因素作为坐标工作:^{} Coordinates should accept single categorical values。结果是您必须将数据放入BokehColumnDataSourceexplicityl中(始终是一个选项),或者在这种情况下,解决方法也只是传递单个项列表:

    p.vbar(x=["cnn"], ...)
    

以下是您代码的完整更新,其中包含一些伪数据:

from bokeh.plotting import figure
from bokeh.io import show

p = figure(x_range=["cnn"])

p.vbar(x=["cnn"], width=0.5, bottom=0, top=10, color="firebrick")

show(p)

enter image description here

我还建议学习用户指南部分Handling Categorical Data。在

相关问题 更多 >