python - 散景:限制字符串列表的绘图平移和缩放
问题描述
尝试以下操作:将散景图平移限制在定义的范围内,它限制了 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)
解决方案
弄清楚了:
使用 FactorRange 并将边界设置为 auto:
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')
推荐阅读
- c++ - 如何使用 OpenGL 和 Qt 移动场景视图?
- asp.net - jenkins 部署后 15 分钟后回收应用程序池
- javascript - Javascript:同步执行
- graphql - 我应该如何解决石墨烯中带有前导下划线的字段?
- c++ - 为什么 c++ 使用 CustomClass** 而不是 CustomClass* 来创建返回 CustomClass 指针列表的函数?
- gulp - Gulp-inject 注入文件路径错误
- javascript - Javascript数组播放声音
- javascript - ANGULAR 动态表单 - 使用 ASYNC 数据失败
- javascript - 执行嵌套导出时出现错误“模块的默认导出具有或正在使用私有名称”
- c# - 将 Json 字符串数组转换为列表