python - 并排条形图
问题描述
seaborn
我正在尝试使用and创建这种“并排”条形图pandas
。
这就是我创建数据框的方式:
dfs = pd.DataFrame(data={'investors': ['first','second','third'], 'stocks': [23, 123, 54], 'bonds': [54, 67, 123], 'real estate': [45, 243, 23]})
这是条形图代码:
sns.factorplot(x='investors', y='bonds', data=dfs, kind='bar')
有人可以帮忙吗?谢谢
解决方案
在你的日期框架上使用melt
,然后用 seaborn 绘制它。
更新:在较新版本的 seaborn 中,factorplot 已更改为 catplot。
sns.catplot(x = 'investors', y='value',
hue = 'investments',data=dfs1,
kind='bar')
# import libraries
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
dfs = pd.DataFrame(data={'investors': ['first','second','third'],
'stocks': [23, 123, 54],
'bonds': [54, 67, 123],
'real estate': [45, 243, 23]})
dfs1 = pd.melt(dfs, id_vars = "investors")
print(dfs1)
investors investments value
0 first stocks 23
1 second stocks 123
2 third stocks 54
3 first bonds 54
4 second bonds 67
5 third bonds 123
6 first real estate 45
7 second real estate 243
8 third real estate 23
sns.factorplot(x = 'investors', y='value',
hue = 'investments',data=dfs1, kind='bar')
plt.show()
输出 :-
推荐阅读
- r - 查找对应于给定百分位数的 DOY
- javascript - Typescript error for type Array:- Cannot invoke an expression whose type lacks a call signature
- git - Github - 恢复拉动
- php - php 代码在运行 php 文件时无法正确执行 php 代码显示在网页上方
- javascript - chaining reduce() and map() Javascript
- c++ - 为什么 folly::future BrokenPromise 使 const char* 构造函数显式?
- sql - spark-sql 面临的奇怪问题
- vb.net - 以速率 n 设置而不是速率并除以日期
- java - 如何使用java将两种不同大小的列表发送到excel中
- node.js - 如何在 Vagrant VM 上安装 bcrypt?