具有名义或顺序轴类型的Bokeh图表

2 投票
1 回答
5513 浏览
提问于 2025-04-18 02:48

编辑:原问题中的代码是几年前的Bokeh版本,下面的回答已经更新,以适应现代版本的Bokeh。


使用名义轴类型的Bokeh图表

from bokeh.plotting import *
from bokeh.objects import *
output_notebook()

label = ['United States', 'Russia', 'South Africa', 'Europe (average)', 'Canada', 'Austalia', 'Japan']
number = [1, 2, 3, 4, 5, 6, 7]
value = [700, 530, 400, 150, 125, 125, 75]
yr = Range1d(start=0, end=800)

figure(y_range=yr)
rect(number, [x/2 for x in value] , width=0.5, height=value, color = "#ff1200")
show()

我想在一个Bokeh图表的条形图中给每个条形标上地区的名称。请问我该如何绘制一个包含类别(名义或顺序)的图表呢?可以参考这个例子 http://en.wikipedia.org/wiki/File:Incarceration_Rates_Worldwide_ZP.svg

注意:我使用的是Python v.2.7.6IPython v.1.2.1

1 个回答

10

你需要把标签传递为 x_range=label,然后用 p.xaxis.major_label_orientation 来设置标签的方向...

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

output_file("bar.html")

label = ['United States', 'Russia', 'South Africa', 'Europe (average)', 'Canada', 'Austalia', 'Japan']
value = [700, 530, 400, 150, 125, 125, 75]

p = figure(x_range=label, y_range=(0, 800))
p.xaxis.major_label_orientation = np.pi/4   # radians, "horizontal", "vertical", "normal"

p.vbar(x=label, top=value , width=0.5, color = "#ff1200")

show(p)

这里输入图片描述

谢谢。

撰写回答