散景:极限图平移和缩放以获取字符串列表

时间:2018-08-17 23:29:20

标签: python bokeh

尝试以下方法:Limit bokeh plot pan to defined range,它限制了x轴的范围,如何在y轴是字符串列表的y轴上做到这一点?

代码:

from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource,DataRange1d,Range1d
from bokeh.palettes import GnBu3, OrRd3
from bokeh.plotting import figure

output_file("bar_stacked_split.html")

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]

exports = {'fruits' : fruits,
           '2015'   : [2, 1, 4, 3, 2, 4],
           '2016'   : [5, 3, 4, 2, 4, 6],
           '2017'   : [3, 2, 4, 4, 5, 3]}
imports = {'fruits' : fruits,
           '2015'   : [-1, 0, -1, -3, -2, -1],
           '2016'   : [-2, -1, -3, -1, -2, -2],
           '2017'   : [-1, -2, -1, 0, -2, -2]}


p = figure(y_range=fruits, plot_height=800, plot_width=800, x_range=DataRange1d(bounds=(-16,16)), title="Fruit import/export, by year",
           toolbar_location='below')

p.hbar_stack(years, y='fruits', height=0.9, color=GnBu3, source=ColumnDataSource(exports),
             legend=["%s exports" % x for x in years])

p.hbar_stack(years, y='fruits', height=0.9, color=OrRd3, source=ColumnDataSource(imports),
             legend=["%s imports" % x for x in years])

p.y_range.range_padding = 0.1
p.ygrid.grid_line_color = None
p.legend.location = "top_left"
p.axis.minor_tick_line_color = None
p.outline_line_color = None

show(p)

1 个答案:

答案 0 :(得分:0)

想通了:

使用FactorRange并将范围设置为自动为:

from bokeh.models import FactorRange

p = figure(y_range=FactorRange(factors=fruits,bounds='auto'), plot_height=800, plot_width=1600, x_range=DataRange1d(bounds=(-16,16)), title="Fruit import/export, by year",
       toolbar_location='below')