简体   繁体   中英

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. 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? Sample Graph

Here's a quick 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:

df_chart = df_tip.groupby(['day', 'sex'])['total_bill'].sum().unstack()
print(df_chart)

Output 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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM