【问题标题】:create a heatmap of two categorical variables创建两个分类变量的热图
【发布时间】:2019-03-23 09:57:54
【问题描述】:

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

  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()

但我不知道如何绘制它。

谢谢!

【问题讨论】:

标签: python pandas plot


【解决方案1】:

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

只是创建一个小例子:

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()

结果是:

【讨论】:

    猜你喜欢
    • 2020-11-25
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多