这是this one的后续问题。
我想在热图的单元格中添加文本。我以为我可以使用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)
我看到了热图,但没有标签。如何显示文字?
答案 0 :(得分:1)
共有五个级别:“图像,参考底图,字形,注释,叠加”。 p.rect
的级别为字形,
如果您未设置LabelSet
的level参数,则其级别为批注,它位于
水平的字形。