首页 > 解决方案 > 创建两个分类变量的热图

问题描述

我有以下三个变量的数据集:

  1. df['Score'] 浮点数(1 或 0)
  2. df['Province'] 一个对象列,其中每一行都是一个区域
  3. df['Product type'] 表示行业的对象。

我想创建一个联合图,其中 x 轴上有不同的行业,y 轴上有不同的省份,作为联合图的颜色,我有分数的相对频率。像这样的东西。 https://seaborn.pydata.org/examples/hexbin_marginals.html

暂时只能做到以下几点

mean = df.groupby(['Province', 'Product type'])['score'].mean()

但我不知道如何绘制它。

谢谢!

标签: pythonpandasplot

解决方案


如果您正在寻找热图,您可以使用 seabornheatmap函数。但是,您需要先旋转您的表格。

只是创建一个小例子:

import numpy as np 
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

score = [1, 1, 1, 0, 1, 0, 0, 0]
provinces = ['Place1' ,'Place2' ,'Place2', 'Place3','Place1', 'Place2','Place3','Place1']
products = ['Product1' ,'Product3' ,'Product2', 'Product2','Product1', 'Product2','Product1','Product1']
df = pd.DataFrame({'Province': provinces,
                   'Product type': products,
                   'score': score
                  })

我的df样子:

   'Province''Product type''score'
0   Place1    Product1      1
1   Place2    Product3      1
2   Place2    Product2      1
3   Place3    Product2      0
4   Place1    Product1      1
5   Place2    Product2      0
6   Place3    Product1      0
7   Place1    Product1      0

然后:

df_heatmap = df.pivot_table(values='score',index='Province',columns='Product type',aggfunc=np.mean)
sns.heatmap(df_heatmap,annot=True)
plt.show()

结果是:


推荐阅读