简体   繁体   中英

Bokeh Categorical Data Heatmap Not Showing

I am trying to create a Heatmap from a csv using python pandas. I keep on getting a blank screen for my output, and I not sure why-- I've exhausted the documentation online to try to figure it out. My code is as follows:

import numpy as np
from bokeh.io import output_file, show
from bokeh.models import (
    ColumnDataSource,
    HoverTool,
    LinearColorMapper,
    BasicTicker,
    PrintfTickFormatter,
    ColorBar,
    FactorRange
)
from bokeh.plotting import figure
from bokeh.sampledata.unemployment1948 import data
from bokeh.transform import transform

#Initialize DataFrame
df = pd.read_csv("myData.csv") 

df.dropna().astype(float)
df.columns.name = 'Month'
df.index.name = 'Facility'

df_1 = pd.DataFrame(df.stack(), columns=['state']).reset_index()
source = ColumnDataSource(df_1)  


#Heatmap
colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d"]
mapper = LinearColorMapper(palette=colors, low=df_1.state.min(), high=df_1.state.max())

TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"

p = figure(title="WSSDM4",
           x_range= list(df.columns), y_range=list(df.index),
           x_axis_location="above", plot_width=1800, plot_height=800,
           tools= TOOLS, toolbar_location='below',
           tooltips=[('Facility', '@Facility'), ('Month', '@Month')])

p.rect(x='Facility', y = 'Month',width=1, height=1, source=source,
        line_color=None, fill_color=transform('state', mapper))
   
color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     label_standoff=6, border_line_color=None, location=(0, 0))

p.add_layout(color_bar, 'right')

p.axis.axis_line_color = None
p.axis.major_tick_line_color = None
p.axis.major_label_text_font_size = "7px"
p.axis.major_label_standoff = 0
p.xaxis.major_label_orientation = 1.0
show(p)

I am using Jupyter notebooks and have all the up-to-date libraries.

Thank you!

I believe your problem is this line: df.index.name = 'Facility'

All this would do is name the existing df index (which looks like 0, 1, 2, 3...) "Facility," rather than taking the existing Facility column and making it the index. To do that, you'd want: df = df.set_index('Facility')

I found this discrepancy by adding a bunch of print(df.head()) statements after each line where your code modified the dataset, and doing the same in the original Bokeh heatmap example, and looking for where they diverged. Might be useful in case there are other problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM