Advertisement
elena1234

Plotting histograms from grouped data in Python

Sep 12th, 2023 (edited)
1,134
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | Source Code | 1 0
  1. df.reset_index().pivot('id','cholesterol','age').hist()
  2. plt.show()
  3.  
  4. ############################################################################################
  5.  
  6. plt.figure(figsize=(20,10), dpi = 250)
  7. df.hist('age', by='cholesterol')
  8. plt.show()
  9.  
  10. ##########################################################################################
  11.  
  12. df_age_chol.groupby('age').cholesterol.value_counts().unstack().plot.bar(width=1, stacked=True)
  13. plt.title('Cholesterol Levels by Age')
  14. plt.show()
  15.  
  16. ##########################################################################################
  17.  
  18. df_age_chol = df[['age', 'cholesterol']].copy()
  19. df_age_chol['cholesterol'] = df_age_chol['cholesterol'].replace({1: 'normal', 2: 'above normal',
  20.                                                                  3: 'well above normal'})
  21. g = sns.FacetGrid(df_age_chol, row = 'cholesterol', height = 2, aspect = 3)
  22. g = g.map(plt.hist, 'age')
  23. plt.show()
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement