首页 > 解决方案 > 热图:R/Python 中每列具有不同的颜色和缩放比例

问题描述

在此处输入图像描述 我想生成带注释的热图,其中每列都将具有新颜色。

 <my code>
 ```
 import seaborn as sns
 import matplotlib.pyplot as plt
 df = pd.DataFrame({'clust': ['Clust 10','Clust 11','Clust 1','Clust 2','Clust 10','Clust 11','Clust 1','Clust 2','Clust 10','Clust 11','Clust 1','Clust 2'],'value': [4,2,0,0, 0,0,1,3, 1,0,0,0], 'category':  ['A','A','A','A','B','B','B','B','C','C','C','C']})
 result = df.pivot(index='clust', columns='category',values='value')
 sns.heatmap(result, annot=True, fmt="g", cmap='viridis')
 plt.show()
 ```

 <Input file>
       No     A     B     C
 Clust 10     4     0     1
 Clust 11     2     0     0
 Clust 1      0     1     0
 Clust 2      0     3     0
 Clust 3      3     1     0
 Clust 4      2     0     2

 <Output>
 enter image description here

标签: python-3.xggplot2

解决方案


您可以通过 python 中的 plotly 模块创建热图。下面是生成热图的代码。

import plotly.figure_factory as ff

a = [
[4, 0, 1],
[2, 0, 0],
[0, 1, 0],
[0, 3, 0],
[3, 1, 0],
[2, 0, 2]
]


fig = ff.create_annotated_heatmap(a)
fig.show() 

有关如何生成热图的更多信息,请参阅https://plot.ly/python/annotated-heatmap/ 。

注意:我没有测试过,但此代码仅供参考。


推荐阅读