从 Pandas Dataframe 制作热图表

问题描述 投票:0回答:1

我有一个 Pandas Dataframe,我想从中制作热图。我看到了如何使用 plotly/seaborn 制作热图,但这并没有生成我正在寻找的东西。这张图片更符合我的要求。

这在 excel 中很容易做到,但我不确定如何在 Python 中做到。

python pandas visualization heatmap
1个回答
0
投票

您可以使用 pandas 的 style.background_gradient 方法和从红色到绿色的颜色图。根据值,这将填充数据框中单元格的背景。

from  matplotlib.colors import LinearSegmentedColormap
cm=LinearSegmentedColormap.from_list('rg',["r", "w", "g"], N=25)
display(df.style.background_gradient(cmap=cm))

Output_Dataframe

© www.soinside.com 2019 - 2024. All rights reserved.