visualize_5_29.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. def bardot(df="dataframe", dim=(6, 4), bw=0.4, colorbar="#f2aa4cff", colordot=["#101820ff"], hbsize=4, r=300, ar=0,
  2. dotsize=6, valphabar=1, valphadot=1, markerdot="o", errorbar=True, show=False, ylm=None, axtickfontsize=9,
  3. axtickfontname="Arial", axlabelfontsize=9, axlabelfontname="Arial", yerrlw=None, yerrcw=None, axxlabel=None,
  4. axylabel=None, figtype='png'):
  5. # set axis labels to None
  6. _x = None
  7. _y = None
  8. xbar = np.arange(len(df.columns.to_numpy()))
  9. color_list_bar = colorbar
  10. color_list_dot = colordot
  11. if len(color_list_dot) == 1:
  12. color_list_dot = colordot*len(df.columns.to_numpy())
  13. if theme == 'dark':
  14. general.dark_bg()
  15. plt.subplots(figsize=dim)
  16. if errorbar:
  17. plt.bar(x=xbar, height=df.describe().loc['mean'], yerr=df.sem(), width=bw, color=color_list_bar, capsize=hbsize,
  18. zorder=0, alpha=valphabar, error_kw={'elinewidth': yerrlw, 'capthick': yerrcw})
  19. else:
  20. plt.bar(x=xbar, height=df.describe().loc['mean'], width=bw, color=color_list_bar,
  21. capsize=hbsize,
  22. zorder=0, alpha=valphabar)
  23. plt.xticks(xbar, df.columns.to_numpy(), fontsize=axtickfontsize, rotation=ar, fontname=axtickfontname)
  24. if axxlabel:
  25. _x = axxlabel
  26. if axylabel:
  27. _y = axylabel
  28. general.axis_labels(_x, _y, axlabelfontsize, axlabelfontname)
  29. # ylm must be tuple of start, end, interval
  30. if ylm:
  31. plt.ylim(bottom=ylm[0], top=ylm[1])
  32. plt.yticks(np.arange(ylm[0], ylm[1], ylm[2]), fontsize=axtickfontsize, fontname=axtickfontname)
  33. plt.yticks(fontsize=axtickfontsize, rotation=ar, fontname=axtickfontname)
  34. # add dots
  35. for cols in range(len(df.columns.to_numpy())):
  36. # get markers from here https://matplotlib.org/3.1.1/api/markers_api.html
  37. plt.scatter(x=np.linspace(xbar[cols]-bw/2, xbar[cols]+bw/2, int(df.describe().loc['count'][cols])),
  38. y=df[df.columns[cols]].dropna(), s=dotsize, color=color_list_dot[cols], zorder=1, alpha=valphadot,
  39. marker=markerdot)
  40. general.get_figure(show, r, figtype, 'bardot', theme)