python - 创建两个分类变量的热图
问题描述
我有以下三个变量的数据集:
- df['Score'] 浮点数(1 或 0)
- df['Province'] 一个对象列,其中每一行都是一个区域
- df['Product type'] 表示行业的对象。
我想创建一个联合图,其中 x 轴上有不同的行业,y 轴上有不同的省份,作为联合图的颜色,我有分数的相对频率。像这样的东西。 https://seaborn.pydata.org/examples/hexbin_marginals.html
暂时只能做到以下几点
mean = df.groupby(['Province', 'Product type'])['score'].mean()
但我不知道如何绘制它。
谢谢!
解决方案
如果您正在寻找热图,您可以使用 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()
结果是:
推荐阅读
- c# - 尝试使用数组输入 10 个数字,然后在每次输出后将它们写出来
- c# - NuGet 对新下载的包返回“包完整性检查”
- javascript - 无法在 Dynatrace 中执行 Javascript 正则表达式
- elasticsearch - 全文搜索引擎和安全性
- sql-server - 需要帮助理解在多对多表之间建立关系的正确方法
- python - weblogic服务器状态控制脚本
- react-native - React Native FlatList 给出错误,它没有嵌套在 scrollView 中
- r - 如何将分类因子水平更改为 R 中的数值变量
- amazon-web-services - 使用 ECS-CLI 将多容器 docker 应用程序部署到 AWS ECS Fargate
- c - 在 C 中交换两个值