Heatmap does not show

醉酒当歌 提交于 2019-12-11 14:23:21

问题


I am trying to plot a simple heatmap from a dataframe that looks like this:

   row column content amount
0    x      a      c1      1
2    x      b      c3      3
4    x      c      c2      1
6    y      a      c1      1
8    y      b      c3      3
10   y      c      c2      1
12   z      a      c1      1
14   z      b      c3      3
16   z      c      c2      1

row and column indicate the position of the cell, the color of it should be chosen based on content and I want tooltips displaying the content and the amount.

I currently try it like this (using bokeh 1.2.0):

import pandas as pd

from bokeh.io import show
from bokeh.models import CategoricalColorMapper, LinearColorMapper, BasicTicker, PrintfTickFormatter, ColorBar, ColumnDataSource
from bokeh.plotting import figure
from bokeh.palettes import all_palettes
from bokeh.transform import transform

df = pd.DataFrame({
    'row': list('xxxxxxyyyyyyzzzzzz'),
    'column': list('aabbccaabbccaabbcc'),
    'content': ['c1', 'c2', 'c3', 'c1', 'c2', 'c3'] * 3,
    'amount': list('123212123212123212')})

df = df.drop_duplicates(subset=['row', 'column'])

source = ColumnDataSource(df)

rows = df['row'].unique()
columns = df['column'].unique()
content = df['content'].unique()

colors = all_palettes['Viridis'][max(len(content), 3)]
mapper = CategoricalColorMapper(palette=colors, factors=content)

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

p = figure(title="My great heatmap",
           x_range=columns, y_range=rows,
           x_axis_location="above", plot_width=600, plot_height=400,
           tools=TOOLS, toolbar_location='below',
           tooltips=[('cell content', '@content'), ('amount', '@amount')])

p.grid.grid_line_color = None
p.axis.axis_line_color = None
p.axis.major_tick_line_color = None
p.axis.major_label_text_font_size = "5pt"
p.axis.major_label_standoff = 0

p.rect(x="row", y="column", width=1, height=1,
       source=source,
       fill_color=transform('content', mapper))

# color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="5pt",
#                      location=(0, 0))
# p.add_layout(color_bar, 'right')

show(p)

However, there are two issues:

1) When executed, I get an empty heatmap:

Any ideas why?

2) When I outcomment the color_bar = ... part, I receive an error saying:

ValueError: expected an instance of type ContinuousColorMapper, got CategoricalColorMapper(id='3820', ...) of type CategoricalColorMapper

What am I doing wrong?


回答1:


Your x and y coordindates are swapped, should be:

p.rect(x="column", y="row", ...)

As for the other message, it is self-explanatory: As of Bokeh 1.2, ColorBar can only be configured with continuous color mappers (e.g. LinearColorMapper). You can either:

  • compute colors yourself in Python code, and include a column of colors in source, or
  • re-cast your plot to use a LinearColorMapper (i.e. map content appropriately to some numerical scale)



回答2:


For your colorBar the solution is here, I quiet did not understand yet what happened to your source, I will dig a bit deeper another time. The colorBar expected a continuous mapper you gave it a categorical.

from bokeh.models import (CategoricalColorMapper, LinearColorMapper, 
BasicTicker, PrintfTickFormatter, ColorBar, ColumnDataSource, 
LinearColorMapper)


factors =df['content'].unique().tolist()
colors = all_palettes['Viridis'][max(len(factors), 3)]
mapper = LinearColorMapper(palette=colors)


来源:https://stackoverflow.com/questions/56754158/heatmap-does-not-show

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!