Xhoan
Xhoan

Reputation: 330

Python Bokeh Getting Empty Heatmap

i'm attempting to make a HeatMap just like this one using Bokeh. Here is my dataframe Data from which i'm trying to make the 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
 ...

And here is my code:

 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)

When i excecute my code i don't get any errors but i just get an empty Frame, as shown in the image below.

HeatMap

I've been struggling trying to find where is my mistake, ¿Why i'm getting this? ¿Where is my error?

Upvotes: 1

Views: 510

Answers (1)

DuCorey
DuCorey

Reputation: 895

The problem with your code is the data type that you are setting for the x and y axis range and the data type of your ColumnDataSource are different. You are setting the x_range and y_range to be a list of strings, but from looking at your data in csv format it will be treated as integers.

In your case, you would want to make sure that your Day and Code column are in string format.

This can be easily done using the pandas package with

Data['Day'] = Data['Day'].astype('str')
Data['Code'] = Date['Code'].astype('str')

Upvotes: 1

Related Questions