Bokeh bar ch中棒材的选择顺序

2024-05-20 15:47:11 发布

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

作为学习使用Bokeh的一部分,我试着做一个简单的条形图。我按一定的顺序(一周中的几天)传递这些标签,而Bokeh似乎在按字母顺序对它们进行排序。我怎样才能让这些条按原始列表的顺序显示?在

from bokeh.plotting import show
from bokeh.io import output_notebook
from bokeh.charts import Bar
from collections import OrderedDict
import calendar 

output_notebook()

data = OrderedDict()
data['values'] = [2,3,4,5,6,7,8] #values only ascending to make correct graph clear
data['days'] = [calendar.day_name[i-1] for i in range(7)]
p = Bar(data, label='days', values='values', 
         title='OrderedDict Input',xlabel="Day", ylabel="Value")
show(p)

Output generated


Tags: fromimportoutputdata顺序showbokehbar
3条回答

一般来说,对于任何绘图,您都应该能够显式地指定x(或y)范围。^如果您想完全忽略Barchart类,{a1}是很有帮助的(因为前面提到的原因,它不是世界上最糟糕的想法)。user666's answer如果您的数据列已经按您希望的方式排序,则很有帮助。否则,您可以自己指定顺序:

本周从周日开始:

from bokeh.models import FactorRange
...
p.x_range = FactorRange(factors=data['days'])

Sunday start

星期一开始:

^{pr2}$

enter image description here

Bokeh项目维护人员注意:这个答案指的是一个过时和不推荐的API,不应该在任何新代码中使用。有关使用现代且完全受支持的Bokeh api创建条形图的信息,请参阅其他响应。在


下面是如何使用Charts接口在您的示例中保留标签的原始顺序,该接口使用bokeh0.11.1进行了测试。在

from bokeh.plotting import show
from bokeh.io import output_notebook
from bokeh.charts import Bar
from collections import OrderedDict
import calendar 
from bokeh.charts.attributes import CatAttr

output_notebook()

data = OrderedDict()
data['values'] = [2,3,4,5,6,7,8] #values only ascending to make correct graph clear
data['days'] = [calendar.day_name[i-1] for i in range(7)]
p = Bar(data, label=CatAttr(columns=['days'], sort=False), 
        values='values',title='OrderedDict Input',xlabel="Day", ylabel="Value")
show(p)

我不太喜欢像条形图这样的高级图表。它们不是很可定制的。 “用手”建造它们通常更容易,而且不需要太长时间。我会这样做:

from bokeh.plotting import figure
from bokeh.io import output_file, show
import calendar

values = [2,3,4,5,6,7,8]
days = [calendar.day_name[i-1] for i in range(1,8)]

p = figure(x_range=days)
p.vbar(x=days, width=0.5, top=values, color = "#ff1200")

output_file('foo.html')
show(p)

结果是:

enter image description here

相关问题 更多 >