Guest User

Untitled

a guest
Feb 17th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. from math import ceil
  2. from scipy.stats import norm
  3. from scipy import stats
  4.  
  5. def plot_normal(df, sub_col):
  6. """ Plot QQ-plot alongside histogram."""
  7. length = ceil(len(sub_col)/2)
  8.  
  9. fig, axes = plt.subplots(length, 4, figsize=(20,17))
  10. axes = axes.ravel()
  11.  
  12. index = 0
  13. for col in sub_col:
  14. (mu, sigma) = norm.fit(df[col])
  15. (a,b) = stats.probplot(df[col], sparams=(mu, sigma)) # QQ plot
  16. axes[index].plot(*a, color='#014d64', linewidth=5)
  17. (x_min, x_max) = axes[index].get_xlim()
  18. x_dots = np.arange(x_min, x_max)
  19. y_dots = b[0]*x_dots + b[1]
  20. axes[index].plot(x_dots, y_dots, color='#7c260b')
  21. axes[index].set_title('QQ Plot', fontsize='large')
  22. axes[index].set_xlabel('Theoretical Quantiles')
  23. axes[index].set_ylabel('Ordered Values')
  24.  
  25. index += 1
  26. sns.distplot(df[col], ax=axes[index], kde=True, color='#c7e1e8',
  27. kde_kws={'color':'#014d64','label':'KDE'},
  28. fit=norm, fit_kws={'color':'#7c260b','label':'NORMAL'}) # distribution plot
  29. axes[index].legend(loc='best')
  30. axes[index].set_title('Histogram', fontsize='large')
  31. index += 1
Add Comment
Please, Sign In to add comment