visualize_5_37.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. def boxplot_single_factor(df='dataframe', column_names=None, grid=False, ar=(0, 0), axtickfontsize=9,
  2. axtickfontname='Arial', dim=(6, 4), show=False, figtype='png', figname='boxplot', r=300,
  3. ylm=None, box_line_style='-', box_line_width=1, box_line_color='b', med_line_style='-',
  4. med_line_width=1, med_line_color='g', whisk_line_color='b', cap_color='b',
  5. add_sign_symbol=False, symb_dist=None, sign_symbol_opts={'symbol': '*', 'fontsize': 8 },
  6. pv=None, notch=False, outliers=True, fill_box_color=True, dotplot=False, dotsize=6,
  7. colordot=['#101820ff'], valphadot=1, markerdot='o', theme=None):
  8. if theme == 'dark':
  9. general.dark_bg()
  10. plt.subplots()
  11. if column_names:
  12. xbar = column_names
  13. else:
  14. xbar = list(df.columns)
  15. # rot is x axis rotation
  16. other_args = {'grid': grid, 'rot': ar[0], 'fontsize': axtickfontsize, 'notch':notch, 'showfliers':outliers,
  17. 'figsize': dim, 'patch_artist': fill_box_color}
  18. color_args = {'medians': med_line_color, 'boxes': box_line_color, 'whiskers': whisk_line_color,
  19. 'caps': cap_color}
  20. medianprops_args = {'linestyle': med_line_style, 'linewidth': med_line_width}
  21. boxprops_args = {'linestyle': box_line_style, 'linewidth': box_line_width}
  22. if isinstance(column_names, list):
  23. df.boxplot(column=column_names, **other_args, boxprops=boxprops_args, medianprops=medianprops_args,
  24. color=color_args)
  25. else:
  26. df.boxplot(**other_args, boxprops=boxprops_args, color=color_args, medianprops=medianprops_args)
  27. # ylm must be tuple of start, end, interval
  28. if ylm:
  29. plt.ylim(bottom=ylm[0], top=ylm[1])
  30. plt.yticks(np.arange(ylm[0], ylm[1], ylm[2]), fontsize=axtickfontsize, fontname=axtickfontname)
  31. plt.yticks(fontsize=axtickfontsize, rotation=ar[1], fontname=axtickfontname)
  32. color_list_dot = colordot
  33. if len(color_list_dot) == 1:
  34. color_list_dot = colordot * len(xbar)
  35. # checked for unstacked data
  36. if dotplot:
  37. for cols in range(len(xbar)):
  38. plt.scatter(
  39. x=np.linspace(xbar[cols] - bw / 2, xbar[cols] + bw / 2, int(bar_counts[cols])),
  40. y=df[df.columns[cols]].dropna(), s=dotsize, color=color_list_dot[cols], zorder=10, alpha=valphadot,
  41. marker=markerdot)
  42. size_factor_to_start_line = max(df.max()) / 20
  43. if add_sign_symbol:
  44. # p and symb_dist should be dict
  45. if isinstance(pv, dict):
  46. for k, v in pv.items():
  47. if isinstance(symb_dist, dict):
  48. if k not in symb_dist:
  49. symb_dist[k] = 0
  50. y_pos = df[k].max() + size_factor_to_start_line + symb_dist[k]
  51. else:
  52. y_pos = df[k].max() + size_factor_to_start_line
  53. if y_pos > 0 and v <= 0.05:
  54. pv_symb = general.pvalue_symbol(v, sign_symbol_opts['symbol'])
  55. if pv_symb:
  56. plt.annotate(pv_symb, xy=((xbar.index(k))+1, y_pos),
  57. fontsize=sign_symbol_opts['fontsize'],
  58. ha="center")
  59. general.get_figure(show, r, figtype, figname, theme)