Python 散景变空热图

Python Bokeh Getting Empty Heatmap

我正在尝试使用 Bokeh 制作热图,就像 this one 一样。 这是我的数据框 Data,我试图从中制作 HeatMap

    Day Code    Total
 0  1   6001    44
 1  1   6002    40
 2  1   6006    8
 3  1   6008    2
 4  1   6010    38
 5  1   6011    1
 6  1   6014    19
 7  1   6018    1
 8  1   6019    1
 9  1   6023    10
 10 1   6028    4
 11 2   6001    17
 12 2   6010    2
 13 2   6014    4
 14 2   6020    1
 15 2   6028    2
 16 3   6001    48
 17 3   6002    24
 18 3   6003    1
 19 3   6005    1
 20 3   6006    2
 21 3   6008    18
 22 3   6010    75
 23 3   6011    1
 24 3   6014    72
 25 3   6023    34
 26 3   6028    1
 27 3   6038    3
 28 4   6001    19
 29 4   6002    105
 30 5   6001    52
 ...

这是我的代码:

 from bokeh.io import output_file
 from bokeh.io import show
 from bokeh.models import (
     ColumnDataSource,
     HoverTool,
     LinearColorMapper
 )
 from bokeh.plotting import figure

 output_file('SHM_Test.html', title='SHM', mode='inline')

 source = ColumnDataSource(Data)
 TOOLS = "hover,save"

 # Creating the Figure
 SHM = figure(title="HeatMap",
       x_range=[str(i) for i in range(1,32)], 
       y_range=[str(i) for i in range(6043,6000,-1)],
       x_axis_location="above", plot_width=400, plot_height=970,
       tools=TOOLS, toolbar_location='right')

 # Figure Styling
 SHM.grid.grid_line_color = None
 SHM.axis.axis_line_color = None
 SHM.axis.major_tick_line_color = None
 SHM.axis.major_label_text_font_size = "5pt"
 SHM.axis.major_label_standoff = 0
 SHM.toolbar.logo = None
 SHM.title.text_alpha = 0.3

 # Color Mapping
 CM = LinearColorMapper(palette='RdPu9', low=Data.Total.min(), high=Data.Total.max())

 SHM.rect(x='Day', y="Code", width=1, height=1,source=source,
          fill_color={'field': 'Total','transform': CM})

 show(SHM)

当我执行我的代码时,我没有得到任何错误,但我只是得到一个空框架,如下图所示。

我一直在努力寻找我的错误在哪里,¿为什么我会得到这个? ¿我的错误在哪里?

您的代码存在问题,您为 x 和 y 轴范围设置的数据类型与 ColumnDataSource 的数据类型不同。您正在将 x_rangey_range 设置为字符串列表,但是通过查看 csv 格式的数据,它将被视为整数。

对于您的情况,您需要确保日期和代码列在 字符串格式。

这可以使用 pandas 包和

轻松完成
Data['Day'] = Data['Day'].astype('str')
Data['Code'] = Date['Code'].astype('str')