具有分类数据的 hbar bokeh 1.3.4

hbar bokeh 1.3.4 with categorical data

我正在尝试在散景中使用 hbar 构建一个分类图,尽管它不遵循与 vbar 相同的概念似乎有点奇怪。我尝试了一些变体,但仍然无法绘制数据,我只得到一个空 canvas。如果有人可以帮助我,我将不胜感激。 我在我的系统和我在 Flask 中构建的 webapp 中使用 bokeh 1.3.4,所以它必须是这个版本或更低版本(感觉有点苛刻,但它是软件要求)。 我已经用 pandas_bokeh 完成了它,这使得它非常简单,尽管我在情节中添加了交互性以允许观众玩耍并且 pandas_bokeh 完成了这项工作,但你最终没有正确学习它。 webapp draft so far

rX = df3.index.values
xL = ['Dublin New', 'Ireland New','Dublin Existing','Ireland Existing']
labelDict = {'2010': xL, '2011': xL, '2012': xL, '2013': xL, '2014': xL,'2015': xL,'2016': xL,'2017': xL, '2018': xL}

sourceT = ColumnDataSource(data=dict(x=df3.index.values,
                                    y=df3['Dublin New'],
                                    y1=df3['Ireland New'],
                                    y2=df3['Dublin Existing'],
                                    y3=df3['Ireland Existing']))

pT = figure(y_range=FactorRange(*labelDict), plot_height=350, plot_width=550, title='Properties Transactions in Ireland', tools='pan, wheel_zoom, box_zoom, reset')

pT.hbar(y=dodge('x', -0.5, range=pT.y_range), height=0.3, right='y', fill_color="#FDE724", source=sourceT)
#pT.hbar(y=dodge('x', -0.25, range=pT.y_range), height=0.3, right='y1', fill_color='#35B778', source=sourceT)
show(pT)

这是我想使用散景而不是 pandas_bokeh 重现的情节。

在此先感谢您的帮助。

dodgerange参数的值应该是实际范围:

range=pT.y_range   # GOOD

您正在超出范围 end 属性,这是一个数字:

range=pT.y_range.end  # BAD

编辑:没有一个完整的、最小的复制器,就不可能直接修复你的代码。可以提供的最好的是一个完整的工作示例,它证明 hbar 确实与 vbar 完全等效,通过比较希望对您有用,找出您的完整代码在哪里:

from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.transform import dodge

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

data = {'fruits' : fruits,
        '2015'   : [2, 1, 4, 3, 2, 4],
        '2016'   : [5, 3, 3, 2, 4, 6],
        '2017'   : [3, 2, 4, 4, 5, 3]}

source = ColumnDataSource(data=data)

p = figure(y_range=fruits, x_range=(0, 10), plot_width=250, title="Fruit Counts by Year",
           toolbar_location=None, tools="")

p.hbar(y=dodge('fruits', -0.25, range=p.y_range), right='2015', height=0.2, source=source,
       color="#c9d9d3")

p.hbar(y=dodge('fruits',  0.0,  range=p.y_range), right='2016', height=0.2, source=source,
       color="#718dbf")

p.hbar(y=dodge('fruits',  0.25, range=p.y_range), right='2017', height=0.2, source=source,
       color="#e84d60")

p.y_range.range_padding = 0.1
p.ygrid.grid_line_color = None

show(p)

@bigreddot 非常感谢。我遵循了您的方法,并且运行顺利。我将尝试保留轴,因为我宁愿在 yaxis 中保留这些年。 无论如何,我真的很感谢你的帮助。

varT = ['Dublin New', 'Ireland New', 'Dublin Existing','Ireland Existing']
yearsT = df3.index.values.tolist()

dataT = {'var': var,
        '2010': df3.iloc[0].values, '2011': df3.iloc[1].values,
        '2012': df3.iloc[2].values, '2013': df3.iloc[3].values,
        '2014': df3.iloc[4].values, '2015': df3.iloc[5].values,
        '2016': df3.iloc[6].values, '2017': df3.iloc[7].values,
        '2018': df3.iloc[8].values,
        }

sourceTs = ColumnDataSource(data=dataT)

pT1 = figure(y_range=var, x_range=(0, df3.values.max()), plot_width=450, title='Properties Transactions in Ireland', tools='pan, wheel_zoom, box_zoom, reset')
pT1.hbar(y=dodge('var', -0.4, range=pT1.y_range), right='2010', height=0.1, source=sourceTs, color='#440154', legend=value('2010'))
pT1.hbar(y=dodge('var', -0.3, range=pT1.y_range), right='2011', height=0.1, source=sourceTs, color='#46317E', legend=value('2011'))
pT1.hbar(y=dodge('var', -0.2, range=pT1.y_range), right='2012', height=0.1, source=sourceTs, color='#365A8C', legend=value('2012'))
pT1.hbar(y=dodge('var', -0.1, range=pT1.y_range), right='2013', height=0.1, source=sourceTs, color='#277E8E', legend=value('2013'))
pT1.hbar(y=dodge('var', 0, range=pT1.y_range), right='2014', height=0.1, source=sourceTs, color='#1EA087', legend=value('2014'))
pT1.hbar(y=dodge('var', 0.1, range=pT1.y_range), right='2015', height=0.1, source=sourceTs, color='#49C16D', legend=value('2015'))
pT1.hbar(y=dodge('var', 0.2, range=pT1.y_range), right='2016', height=0.1, source=sourceTs, color='#9DD93A', legend=value('2016'))
pT1.hbar(y=dodge('var', 0.3, range=pT1.y_range), right='2017', height=0.1, source=sourceTs, color='#FDE724', legend=value('2017'))
pT1.hbar(y=dodge('var', 0.4, range=pT1.y_range), right='2018', height=0.1, source=sourceTs, color='#AADB32', legend=value('2018'))
pT1.legend.location='bottom_right'
#pT1.y_range.range_padding = 0.1
pT1.grid.grid_line_color = None
tick_labels_pt = {'10000':'10K','20000':'20K','30000':'30K','40000':'40K','50000':'50K'}
pT1.xaxis.major_label_overrides = tick_labels_pt
pT1.legend.background_fill_alpha=None
pT1.legend.border_line_alpha=0
pT1.legend.label_text_font_size = "11px"
pT1.legend.click_policy="hide"
pT1.title.text_font_size = '15px'
pT1.axis.major_label_text_font_style = 'bold'
#pT1.xaxis.major_label_text_font_style = 'bold'
pT1.toolbar.autohide = True

show(pT1)

结果如下:

轴反转:

varpti = ['Dublin New', 'Ireland New', 'Dublin Existing','Ireland Existing']
#the values of the y axis has to be in str format
yearspti = '2010','2011','2012', '2013', '2014', '2015', '2016', '2017', '2018' #df3.index.values.tolist()

datapti = {'years': yearspti,
           'Dublin New': df3['Dublin New'].values,
           'Ireland New': df3['Ireland New'].values,
           'Dublin Existing': df3['Dublin Existing'].values,
           'Ireland Existing': df3['Ireland Existing'].values
        }

sourcepti = ColumnDataSource(data=datapti)

pti = figure(y_range=yearspti, x_range=(0, df3.values.max()), plot_height=500, plot_width=450, title='Properties Transactions in Ireland', tools='pan, wheel_zoom, box_zoom, reset')
pti.hbar(y=dodge('years', -0.2, range=pti.y_range), right='Dublin New', height=0.1, source=sourcepti, color='#440154', legend=value('Dublin New'))
pti.hbar(y=dodge('years', 0, range=pti.y_range), right='Ireland New', height=0.1, source=sourcepti, color='#30678D', legend=value('Ireland New'))
pti.hbar(y=dodge('years', 0.2, range=pti.y_range), right='Dublin Existing', height=0.1, source=sourcepti, color='#35B778', legend=value('Dublin Exsiting')) 
pti.hbar(y=dodge('years', 0.4, range=pti.y_range), right='Ireland Existing', height=0.1, source=sourcepti, color='#FDE724', legend=value('Ireland Exsiting'))