我有一个 Pandas Dataframe,我想从中制作热图。我看到了如何使用 plotly/seaborn 制作热图,但这并没有生成我正在寻找的东西。这张图片更符合我的要求。
这在 excel 中很容易做到,但我不确定如何在 Python 中做到。
您可以使用 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))