并排BarPlot

时间:2018-08-26 16:12:25

标签: python pandas statistics seaborn

我正在尝试使用seabornpandas来创建这种“并排” barplot。

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

有人可以帮忙吗?谢谢

1 个答案:

答案 0 :(得分:0)

在日期框架上使用melt,然后用seaborn进行绘制。

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

import seaborn as sns
import matplotlib.pyplot as plt
fig = plt.figure()
sns.factorplot(x = 'investors', y='value', hue = 'investments',data=dfs1, kind='bar')
plt.show()

输出:

enter image description here