并排条形图 [英] Side by Side BarPlot
本文介绍了并排条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 seaborn
和 pandas
创建这种并排"条形图.
这是我创建数据框的方式:
dfs = pd.DataFrame(data={'investors': ['first','second','third'], 'stocks': [23, 123, 54], 'bonds': [54, 67, 123], '房地产': [45, 243, 23]})
这是条形图代码:
sns.factorplot(x='investors', y='bonds', data=dfs, kind='bar')
有人可以帮忙吗?谢谢
解决方案
使用
I'm trying to create this kind of "side by side" barplot with seaborn
and pandas
.
this is how I create data frame:
dfs = pd.DataFrame(data={'investors': ['first','second','third'], 'stocks': [23, 123, 54], 'bonds': [54, 67, 123], 'real estate': [45, 243, 23]})
And here is barplot code:
sns.factorplot(x='investors', y='bonds', data=dfs, kind='bar')
Can anyone please help? Thanks
解决方案
Use melt
on your dateframe then plot it with seaborn.
update: factorplot was changed to catplot in newer versions of seaborn.
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()
output :-
这篇关于并排条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文