python - 如何使用 MatPlotLib 将 Pandas 中的分箱计数显示为热图?

标签 python pandas matplotlib

我在处理 Wine 数据集时注意到,在查看计数时,数字越大显得越暗,并导致某种热图效应。我想知道是否有办法使用 MatPlotLib 来增强效果。

BINS = [0, 50, 100, 150, 200, 1000]
price_by_points = first150.groupby(['points', pd.cut(first150['price'], BINS)]).size().unstack('price').fillna(0)

产生:

price   (0, 50]  (50, 100]  (100, 150]  (150, 200]  (200, 1000]
points                                                         
80        871.0       12.0         0.0         0.0          0.0
81       1444.0       23.0         0.0         0.0          2.0
82       3874.0       55.0         4.0         0.0          0.0
83       5717.0       84.0         3.0         1.0          2.0
84       9861.0      238.0        15.0         4.0          2.0
85      11313.0      262.0        21.0         7.0          1.0
86      13874.0      447.0        26.0         1.0          3.0
87      18097.0      857.0        55.0        12.0          4.0
88      14811.0     1122.0        91.0         7.0          3.0
89      10194.0     1238.0        97.0        13.0         15.0
90      11909.0     2244.0       180.0        45.0         26.0
91       6810.0     2148.0       209.0        28.0         15.0
92       5135.0     2575.0       287.0        74.0         48.0
93       2432.0     2162.0       399.0        98.0        106.0
94       1067.0     1361.0       338.0       100.0        140.0
95        322.0      696.0       205.0        81.0        162.0
96         77.0      262.0       100.0        48.0         78.0
97          9.0      117.0        59.0        29.0         88.0
98          1.0       36.0        29.0        10.0         37.0
99          0.0        8.0         5.0         5.0         21.0
100         0.0        5.0         0.0         4.0         12.0

最佳答案

您可以使用 Matplotlib 生成热图并对其进行注释:

import matplotlib.pyplot as plt
import numpy as np

plt.figure()
heatmap = plt.pcolor(df, cmap='viridis')

# Add text
for y in range(df.shape[0]):
    for x in range(df.shape[1]):
        plt.text(x + 0.5, y + 0.5, '{:.0f}'.format(df.iloc[y, x]),
                 color='w',horizontalalignment='center',
                 verticalalignment='center',)
plt.yticks(np.arange(0.5, len(df.index), 1), df.index)
plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns)
plt.colorbar(heatmap)
plt.ylabel('points')
plt.xlabel('price')

Matplotlib heat map

您还可以使用 seaborn 更轻松地获取带注释的热图:

import seaborn as sns

plt.figure()
sns.heatmap(df, annot=True, fmt='d')
plt.xlabel('Price')

Seaborn heat map

Seaborn 在自动格式化文本阴影方面做得很好,因此随着背景颜色的变化可见。

关于python - 如何使用 MatPlotLib 将 Pandas 中的分箱计数显示为热图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55170301/

相关文章:

python - 在 scikit learn 中组合随机森林模型

python - 桶排序以查找附近的几乎重复项

python - Pandas nunique() 仅用于分类列,否则为空?

python - 修改 Matplotlib 中绘制线的属性

matplotlib - imshow 使用 astype(float) 后反转颜色

Python Selenium 套接字错误 - [Errno 61] 连接被拒绝

python - Windows 10 Bash 和 python 路径

python - 通过对列中的前一个值求和并相乘,在 Pandas 中创建一个列

python - 如何使用模零找到给定值的最近除数

python - 如何在水平条形图中添加百分比?