簡體   English   中英

散景:在熱圖單元格中顯示文本

[英]Bokeh: show text in heatmap cells

我制作熱圖:

在此處輸入圖像描述

使用此代碼:

import pandas as pd
from bokeh.models import BasicTicker, ColorBar, LinearColorMapper, PrintfTickFormatter, LabelSet
from bokeh.plotting import figure, show

df = pd.read_csv('data.csv', sep=';')

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

p = figure(title="Heatmap", toolbar_location=None,
           x_range=days, y_range=list(reversed(hours)),
           x_axis_location="above", width=600, height=600)

p.rect(x="startdate_dayweek", y="startdate_hour", width=1, height=1,
       source=df,
       fill_color={'field': 'cpu_sum', 'transform': mapper},
       line_color=None)

show(p)

所以,我想在熱圖單元格中顯示“cpu_sum”值,就像在這個模型上一樣: 在此處輸入圖像描述

我嘗試了這些代碼行,但它不起作用:

labels = LabelSet(x='startdate_dayweek', y='startdate_hour', text='cpu_sum', level='glyph',
                  x_offset=1, y_offset=1, source=df,
                  render_mode='canvas')
p.add_layout(labels)

錯誤信息:

failed to validate LabelSet(id='21897', ...).source: expected an instance of type DataSource, got     startdate_hour startdate_dayweek  cpu_sum
0                0                 1       62
1                0                 2    27999
2                0                 3    27937
[...]
166              9                 6    22027
167              9                 7    33259

[168 rows x 3 columns] of type DataFrame

您必須進行兩項更改:

  1. 確保要添加的值是string類型的,否則會破壞圖形。
  2. 創建一個類似source= ColumnDataSource(df)的 ColumnDataSource 並將其傳遞給LabelSet中的source關鍵字。

您可以在下面看到一個最小示例,其中包含您的列名稱,但包含虛假數據。

import pandas as pd
from bokeh.models import LinearColorMapper, LabelSet, ColumnDataSource
from bokeh.plotting import figure, show, output_notebook
output_notebook()

# create data
days = []
for i in range(7):
    days.extend([i]*24)
hours = list(range(24))*7
df = pd.DataFrame({
    "startdate_dayweek":days,
    "startdate_hour":hours,
    "cpu_sum":list(range(24*7)),
})
df['cpu_str'] = df['cpu_sum'].astype(str)

# create figure
source= ColumnDataSource(df)
colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d"]
mapper = LinearColorMapper(palette=colors, low=df.cpu_sum.min(), high=df.cpu_sum.max())

p = figure(
    title="Heatmap",
    toolbar_location=None,
    x_axis_location="above",
    x_range=(-0.5,6.5),
    y_range=(-0.5,23.5),
    width=600,
    height=600
)

p.rect(
    x="startdate_dayweek",
    y="startdate_hour",
    width=1,
    height=1,
    source=source,
    fill_color={'field': 'cpu_sum', 'transform': mapper},
    line_color=None
)
labels = LabelSet(
    x='startdate_dayweek',
    y='startdate_hour',
    text='cpu_str',
    level='glyph',
    text_align='center',
    y_offset=-7,
    source=source,
    render_mode='canvas'
)
p.add_layout(labels)

show(p)

熱圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM