无法在散景[Pandas]中绘制Hbar

时间:2018-01-30 08:28:53

标签: python pandas jupyter-notebook bokeh

我试图为数据帧绘制HBar。下面的代码适用于另一个数据帧,但对于当前的数据帧,似乎根本没有输出。

以下是代码:

from bokeh.models import ColumnDataSource, DataRange1d, Plot, LinearAxis, Grid,Label,LabelSet,HoverTool
from bokeh.models.glyphs import HBar
from bokeh.resources import INLINE
import bokeh.io
bokeh.io.output_notebook() 
source1 = ColumnDataSource(
        data=dict(
            GID = Customer_rank.GuestId.head(15).tolist(),
            GSale = Customer_rank.FinalSalePrice.head(15).astype(int).tolist()
        )
    )

    GID = Customer_rank.GuestId.tolist()

    # hover = HoverTool(tooltips=[
    # #     ("(x,y)", "(@lat, @lon)"),
    #     ("Merch_ID", "@ID"),
    #     ('Revenue','@Sales'),
    #     ("Visits","@Count")
    # ])

    p = figure(y_range= ID)
    # p.background_fill_color = None
    # p.background_fill_alpha = None
    # labels = LabelSet(x='Count', y='ID', text='Count', level='glyph',
    #                y_offset=5, render_mode='canvas',source=source)
    # p.add_tools(hover)
    p.grid.grid_line_color = None
    p.xaxis.visible = None
    p.yaxis.axis_line_color = None
    p.xaxis.axis_line_color = None
    p.xaxis.major_tick_line_color = None
    p.xaxis.minor_tick_line_color = None
    p.outline_line_alpha = 0.2
    H_1= HBar(y='GID',right='GSale',height = 0.5,fill_color="#6C69EB")
    # H_2 = HBar(y='ID',right='Sales',height = 0.5)
    p.add_glyph(source1, H_1)
    # p_2 = p.add_glyph(source, H_2)


    # p.add_layout(labels)
    show(p)

有问题的数据框(头部)如下:

    GuestId FinalSalePrice
0   d1dd    16609.103745
1   70c1    4374.211920
2   0bfe    3153.746635
3   5360    2972.489390
4   8b7e    2518.886240
5   7170    2350.038275
6   e359    2312.321375
7   14e1    2057.333395
8   1d1c    1858.219285
9   4956    1793.117495
10  d390    1766.650210
11  2e99    1751.953710
12  ade6    1687.164375
13  bb4c    1686.418435
14  9986    1678.981265

我很困惑为什么代码不会渲染图。

编辑: 我已经使用上面的代码生成了包含以下数据帧的图:

Merchants   Total_Count
    0   LP  198787
    1   KC  134952
    2   BDR 123668
    3   JHU 108698
    4   LKW 82122
    5   COL 75449
    6   POW 70844
    7   GTK 69816
    8   INM 37715
    9   PH  13048
    10  BLO 7464
    11  BWF 4324
    12  HO  4233
    13  BKM 2957
    14  BIN 1877

1 个答案:

答案 0 :(得分:0)

我调整了导入如下(在Jupyter笔记本上测试)

from bokeh.models import ColumnDataSource, DataRange1d, Plot, 
LinearAxis, Grid,Label,LabelSet,HoverTool
from bokeh.models.glyphs import HBar
from bokeh.plotting import figure, output_notebook
from bokeh.io import show
output_notebook()

然后使用ID GID更改了行:

p = figure(y_range= GID)

我可以正确地看到Hbar图表(我收到的是show和figure的错误,这些错误没有导入,而ID在注释时没有定义)