热图不显示
Heatmap does not show
我正在尝试从如下所示的数据框中绘制一个简单的热图:
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
和 column
表示单元格的位置,它的颜色应该根据 content
选择,我想要工具提示显示 content
和amount
.
我目前这样尝试(使用 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)
但是,有两个问题:
1) 执行时,我得到一个空的热图:
知道为什么吗?
2) 当我注释掉 color_bar = ...
部分时,我收到一条错误消息:
ValueError: expected an instance of type ContinuousColorMapper, got
CategoricalColorMapper(id='3820', ...) of type CategoricalColorMapper
我做错了什么?
对于你的 colorBar 解决方案在这里,我安静地不明白你的来源发生了什么,我会再深入挖掘一点。
colorBar 需要一个连续的映射器,你给它一个分类。
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)
你的 x 和 y 坐标交换了,应该是:
p.rect(x="column", y="row", ...)
至于另一个消息,它是self-explanatory:从Bokeh 1.2开始,ColorBar
只能配置连续的颜色映射器(例如LinearColorMapper
)。您可以:
- 自己在 Python 代码中计算颜色,并在
source
中包含一列颜色,或者
- re-cast 您的情节使用
LinearColorMapper
(即将 content
适当地映射到某个数字比例)
我正在尝试从如下所示的数据框中绘制一个简单的热图:
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
和 column
表示单元格的位置,它的颜色应该根据 content
选择,我想要工具提示显示 content
和amount
.
我目前这样尝试(使用 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)
但是,有两个问题:
1) 执行时,我得到一个空的热图:
知道为什么吗?
2) 当我注释掉 color_bar = ...
部分时,我收到一条错误消息:
ValueError: expected an instance of type ContinuousColorMapper, got CategoricalColorMapper(id='3820', ...) of type CategoricalColorMapper
我做错了什么?
对于你的 colorBar 解决方案在这里,我安静地不明白你的来源发生了什么,我会再深入挖掘一点。 colorBar 需要一个连续的映射器,你给它一个分类。
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)
你的 x 和 y 坐标交换了,应该是:
p.rect(x="column", y="row", ...)
至于另一个消息,它是self-explanatory:从Bokeh 1.2开始,ColorBar
只能配置连续的颜色映射器(例如LinearColorMapper
)。您可以:
- 自己在 Python 代码中计算颜色,并在
source
中包含一列颜色,或者 - re-cast 您的情节使用
LinearColorMapper
(即将content
适当地映射到某个数字比例)