Reputation: 57
I have CSV data that is read into a Pandas dataframe such as:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("membership-applications.csv", delimiter=";")
df.sort_values(by=['year', 'quarter'], inplace=True, ascending=True)
quarter,year,type,status,total
Q1,2019,new,approved,10
Q1,2019,renewal,approved,30
Q2,2019,new,approved,10
Q2,2019,new,rejected,20
Q2,2019,renewal,0
Q3,2019,new,0
Q3,2019,renewal,0
Q4,2019,new,0
Q4,2019,renewal,0
Q1,2020,new,approved,10
Q1,2020,renewal,approved,50
How can I plot a stacked bar chart based on quarter, year and total (e.g. sum of each of 'new' or 'renewal' including all statuses)? For example,
+------+
+------+ | 10 |
| 10 | +------+ +------+
+------+ | 10 | | |
| | +------+ | 50 |
| 30 | | 20 | | |
| | | | | |
+------+ +------+ +------+
Q1 2019 Q2 2019 Q3 2019 Q4 2019 Q1 2020
Also, based on the same dataframe, how can I a plot multiple-bar chart, for example for Q1 2019, the first bar is 'new' (which a total of 10) and 'renewal' as the next bar?
Something looks like this:
+--+
| |
+--+ |30|
|10| | |
+--+ +--+
Q1 2019
Thanks in advance for your help!!
Upvotes: 2
Views: 728
Reputation: 14949
you can try pivot_table
to reshape the data:
fig = df.pivot_table(index = ['year','quarter'], columns = 'type', values = 'total', dropna=False , fill_value = 0).plot(kind ='bar', stacked = True)
To display bar side-by-side just remove the stack parameter:
fig = df.pivot_table(index = ['year','quarter'], columns = 'type', values = 'total', dropna=False , fill_value = 0).plot(kind ='bar')
plt.xticks(rotation = 30)
Upvotes: 3