热图不sh

2024-05-13 19:03:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个数据帧绘制一个简单的热图,如下所示:

   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

rowcolumn表示单元格的位置,它的颜色应该基于content来选择,我想要显示content和{}的工具提示。在

我现在这样做(使用bokeh 1.2.0):

^{pr2}$

但是,有两个问题:

1)执行时,我得到一个空的热图: enter image description here

有什么想法为什么?在

2)当我对color_bar = ...部分置评时,我收到一个错误消息:

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

我做错什么了?在


Tags: 工具of数据颜色type绘制columncontent
2条回答

对于你的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)

您的xy坐标被交换,应为:

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

至于另一条消息,这是不言而喻的:从bokeh1.2开始,ColorBar只能配置为连续的颜色映射器(例如LinearColorMapper)。您可以:

  • 在Python代码中自己计算颜色,并在source中包含一列颜色,或者
  • 重新转换绘图以使用LinearColorMapper(即将content映射到某个数字比例)

相关问题 更多 >