1

Here's few rows of my 100k lines df: data.head()

my goal is to have 4 grouped bar charts (1row ; 4 col) where :

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()
2
  • Welcome to StackOverflow? You mentioned that you got some code working, but it wasn't as elegant as you wanted it to be. Could you add this code to the question? Thanks! Commented Nov 5, 2020 at 17:49
  • Thank you! I added the code i have so far. Let me know what you think or if you have any suggestions. Commented Nov 5, 2020 at 21:06

1 Answer 1

2

Since no data were provided, I drew multiple graphs using test data from seaborn. pandas plots and subplots can be addressed with the following technique.

import matplotlib.pyplot as plt
# for sample data
import seaborn as sns
tips = sns.load_dataset("tips")
data_A = tips[tips['day'] == 'Sun']
data_B = tips[tips['day'] == 'Sat']
data_C = tips[tips['day'] == 'Thur']
data_D = tips[tips['day'] == 'Fri']
data_A_pivot=data_A.groupby(['time','sex']).sum().unstack().fillna(0)
data_B_pivot=data_B.groupby(['time','sex']).sum().unstack().fillna(0)
data_C_pivot=data_C.groupby(['time','sex']).sum().unstack().fillna(0)
data_D_pivot=data_D.groupby(['time','sex']).sum().unstack().fillna(0)

fig, [ax1,ax2,ax3,ax4] = plt.subplots(nrows=1, ncols=4, figsize=(20,4))

data_A_pivot.plot(kind='bar', ax=ax1)
data_B_pivot.plot(kind='bar', ax=ax2)
data_C_pivot.plot(kind='bar', ax=ax3)
data_D_pivot.plot(kind='bar', ax=ax4)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.