[英]How do I plot this confusing multiple bar plot using Pandas?
As part of a larger data set, I have a column on banks (values being different names) and a column on mortgage status (values: Accepted; Declined).作为更大数据集的一部分,我有一个关于银行的列(值是不同的名称)和一个关于抵押状态的列(值:接受;拒绝)。 I'm having a lot of trouble creating a multiple bar plot that has the Banks as each x-value, and then two bars on each value for the # of accepted vs # of declined.
我在创建一个多条形图时遇到了很多麻烦,该图将银行作为每个 x 值,然后每个值上的两个条形表示接受的数量与拒绝的数量。 Attaching a mock-up of what I want the end result to be, but am lost on how to get there.
附上我想要的最终结果的模型,但我不知道如何到达那里。 Do I need to use groupby?
我需要使用 groupby 吗? Sample Graph
示例图
Here's a quick MVCE:这是一个快速的MVCE:
from seaborn import load_dataset
import pandas as pd
df_tip = load_dataset('tips')
print(df_tip.head())
Output of raw dataframe:原始数据帧的输出:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
Reshape your dataframe to look like this with your grouped bars as columns, and you x-axis as dataframe row indexes:将数据框重塑为如下所示,将分组条作为列,将 x 轴作为数据框行索引:
df_chart = df_tip.groupby(['day', 'sex'])['total_bill'].sum().unstack()
print(df_chart)
Output df_chart:输出 df_chart:
sex Male Female
day
Thur 561.44 534.89
Fri 198.57 127.31
Sat 1227.35 551.05
Sun 1269.46 357.70
Plot with pandas plot:用熊猫图绘制:
df_chart.plot.bar()
Chart:图表:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.