【问题标题】:hbar bokeh 1.3.4 with categorical data带有分类数据的 hbar bokeh 1.3.4
【发布时间】:2020-02-02 05:19:53
【问题描述】:

我正在尝试在散景中使用 hbar 构建分类图,尽管它不遵循与 vbar 相同的概念似乎有点奇怪。我尝试了一些变化,但仍然无法绘制数据,我只得到一个空画布。如果有人可以帮助我,将不胜感激。 我在我的系统和我在 Flask 中构建的 web 应用程序中使用 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 来重现的情节。

提前感谢您的帮助。

【问题讨论】:

    标签: python bar-chart bokeh categorical-data


    【解决方案1】:

    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,这是我所做的尝试之一,不管有没有#BAD 和#Good 它都不会运行。无论如何,谢谢,我现在将对其进行编辑以避免混淆。
    • 嗯,那是我能看到的唯一一件事,这对于部分 sn-p 绝对是错误的。在这一点上,要说更多,需要一个完整的、最小的复制器,它实际上可以直接运行和调查。
    • 非常感谢。我会在几个小时内检查它。如果它仍然不起作用,我将为此创建一个 GitHub 存储库并在此处与所有文件共享它。
    • 谢谢伙计。我将您的答案标记为正确并发布了一个,只是因为它不适合评论部分。
    【解决方案2】:

    @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'))
    

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 2020-05-18
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多