如何向热图单元格添加注释?
How to add annotation to heatmap cells?
这是 的后续问题。
我想向热图中的单元格添加文本。我想我可以按照 here 所述使用 LabelSet
。但是,不幸的是,当我 运行 以下代码时,我没有看到任何标签:
import pandas as pd
from bokeh.io import show
from bokeh.models import (CategoricalColorMapper, LinearColorMapper,
BasicTicker, PrintfTickFormatter, ColorBar,
ColumnDataSource, LabelSet)
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=rows, y_range=columns,
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))
labels = LabelSet(x='row', y='column', text='content', level='glyph',
x_offset=1, y_offset=1, source=source,
render_mode='canvas')
p.add_layout(labels)
show(p)
我看到了热图,但没有标签。如何显示文本?
共有五个级别:"image, underlay, glyph, annotation, overlay"。 p.rect
的等级是字形,
如果不设置LabelSet
的level参数,它的level是annotation,在上面
等级字形。
有趣的是,OP 的代码对我有用。我来这里是因为我遇到了同样的问题。原来注释数据应该是一个字符串。转换 ColumnDataSource() 中的相应列后,我的注释(数字)显示在热图中。
这是
我想向热图中的单元格添加文本。我想我可以按照 here 所述使用 LabelSet
。但是,不幸的是,当我 运行 以下代码时,我没有看到任何标签:
import pandas as pd
from bokeh.io import show
from bokeh.models import (CategoricalColorMapper, LinearColorMapper,
BasicTicker, PrintfTickFormatter, ColorBar,
ColumnDataSource, LabelSet)
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=rows, y_range=columns,
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))
labels = LabelSet(x='row', y='column', text='content', level='glyph',
x_offset=1, y_offset=1, source=source,
render_mode='canvas')
p.add_layout(labels)
show(p)
我看到了热图,但没有标签。如何显示文本?
共有五个级别:"image, underlay, glyph, annotation, overlay"。 p.rect
的等级是字形,
如果不设置LabelSet
的level参数,它的level是annotation,在上面
等级字形。
有趣的是,OP 的代码对我有用。我来这里是因为我遇到了同样的问题。原来注释数据应该是一个字符串。转换 ColumnDataSource() 中的相应列后,我的注释(数字)显示在热图中。