Here's few rows of my 100k lines df:
data.head()
my goal is to have 4 grouped bar charts (1row ; 4 col) where :
- Each chart correspands to a "product family" (I know i have 4 so i can make 4 sub-df)
- "Site" and "year" in x axis,
- "sum of Tonnage" in y axis, Example of a the bar chart I'm trying to get
The closest i got is to have the 4 plots but one under the other. and the code is not as elegant as i want it to be.
I'm a beginner so this might look too easy for you. just bare with me :)
Here's my code:
data_A=data_no_dp.loc[data_no_dp['Product family']=='A'][['id','Site','Tonnage','Année']].drop_duplicates('id')
data_B=data_no_dp.loc[data_no_dp['Product family']=='B'][['id','Site','Tonnage','Année']].drop_duplicates('id')
data_C=data_no_dp.loc[(data_no_dp['Product family']=='C') ][['id','Site','Tonnage','Année']].drop_duplicates('id')
data_D=data_no_dp.loc[(data_no_dp['Product family']=='D') ][['id','Site','Tonnage','Année','Product family']].drop_duplicates('id')
data_A_pivot=data_A.groupby(['Site','Année']).sum().unstack()
data_A_pivot=data_A_pivot['Tonnage'].replace(np.nan,0)
data_B_pivot=data_B.groupby(['Site','Année']).sum().unstack()
data_B_pivot=data_B_pivot['Tonnage'].replace(np.nan,0)
data_C_pivot=data_C.groupby(['Site','Année']).sum().unstack()
data_C_pivot=data_C_pivot['Tonnage'].replace(np.nan,0)
data_D_pivot=data_D.groupby(['Site','Année']).sum().unstack()
data_D_pivot=data_D_pivot['Tonnage'].replace(np.nan,0)
#plt.subplots(1,4, sharey=True, figsize= (20,4))
plt.subplot(2,2,1)
ax1=data_A_pivot.plot(kind='bar')
ax2=data_B_pivot.plot(kind='bar')
ax3=data_C_pivot.plot(kind='bar')
ax4=data_D_pivot.plot(kind='bar')
plt.show()
