python-3.x - 热图: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 中的 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/ 。
注意:我没有测试过,但此代码仅供参考。
推荐阅读
- json - 在 typescript 2.9.2 和 Angular 6 应用程序中导入 json 文件不起作用
- c# - C# 中的“dispose 模式”:为什么我们需要 `if (disposing)` 条件?
- javascript - 如何查找 JSON 嵌套对象
- cordova - 如何修复 Cordova 项目中的“build.gradle”问题?
- javascript - 下载一个松弛文件只返回登录页面?
- javascript - ng-invalid 仅在提交时在角度输入中无效
- python - 从导出的 SavedModel 形成图像有效负载
- visual-studio-code - 在 tasks.json 中重用环境变量
- haskell - 检索元组并对第 n 个元素进行操作
- digital-ocean - 让使用负载均衡器的一台服务器上的加密证书过期