Vla*_*nko 3 python statistics pandas seaborn
我试图创造这种“并排” barplot与seaborn和pandas。
这就是我创建数据框的方式:
dfs = pd.DataFrame(data={'investors': ['first','second','third'], 'stocks': [23, 123, 54], 'bonds': [54, 67, 123], 'real estate': [45, 243, 23]})
Run Code Online (Sandbox Code Playgroud)
这是条形图代码:
sns.factorplot(x='investors', y='bonds', data=dfs, kind='bar')
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?谢谢
melt在您的日期框架上使用,然后用 seaborn 绘制它。
更新:在较新版本的 seaborn 中,factorplot 已更改为 catplot。
sns.catplot(x = 'investors', y='value',
hue = 'investments',data=dfs1,
kind='bar')
Run Code Online (Sandbox Code Playgroud)
# 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()
Run Code Online (Sandbox Code Playgroud)
输出 :-
| 归档时间: |
|
| 查看次数: |
7805 次 |
| 最近记录: |