python - 并排条形图

标签 python pandas statistics seaborn

我正在尝试使用 seaborn 创建这种“并排”条形图和 pandas .

enter image description here

这就是我创建数据框的方式:

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()
输出 :-
barplot

关于python - 并排条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52028043/

相关文章:

database-design - 基于游戏服务器日志事件的玩家统计数据的 MongoDB 架构设计?

r - 为什么R的物流增长产出与另一个不同?

python - 数组的分布图

python - 使用内部连接连接两个数据框

python - scipy.sparse.csr_matrix 数据的意外行为

python - 如何在 Docker 中使用多种编程语言?

pandas - 在seaborn中通过平滑绘制facetgrid图

python - 通过重复最后一行来增加数据帧中每 1 秒的采样频率

python - 过滤 df 中的行并在字符串值之间返回 - pandas

python - 如何将一串Python代码编译成一个可以调用函数的模块?