visualize_5_40.py 1.6 KB

1234567891011121314151617181920212223242526
  1. def pcaplot(x=None, y=None, z=None, labels=None, var1=None, var2=None, var3=None, axlabelfontsize=9,
  2. axlabelfontname="Arial", figtype='png', r=300, show=False, plotlabels=True, dim=(6, 4), theme=None):
  3. if theme == 'dark':
  4. general.dark_bg()
  5. if x is not None and y is not None and z is None:
  6. assert var1 is not None and var2 is not None and labels is not None, "var1 or var2 variable or labels are missing"
  7. plt.subplots(figsize=dim)
  8. for i, varnames in enumerate(labels):
  9. plt.scatter(x[i], y[i])
  10. if plotlabels:
  11. plt.text(x[i], y[i], varnames, fontsize=10)
  12. general.axis_labels("PC1 ({}%)".format(var1), "PC2 ({}%)".format(var2), axlabelfontsize, axlabelfontname)
  13. general.get_figure(show, r, figtype, 'pcaplot_2d', theme)
  14. elif x is not None and y is not None and z is not None:
  15. assert var1 and var2 and var3 and labels is not None, "var1 or var2 or var3 or labels are missing"
  16. # for 3d plot
  17. fig = plt.figure(figsize=dim)
  18. ax = fig.add_subplot(111, projection='3d')
  19. for i, varnames in enumerate(labels):
  20. ax.scatter(x[i], y[i], z[i])
  21. if plotlabels:
  22. ax.text(x[i], y[i], z[i], varnames, fontsize=10)
  23. ax.set_xlabel("PC1 ({}%)".format(var1), fontsize=axlabelfontsize, fontname=axlabelfontname)
  24. ax.set_ylabel("PC2 ({}%)".format(var2), fontsize=axlabelfontsize, fontname=axlabelfontname)
  25. ax.set_zlabel("PC3 ({}%)".format(var3), fontsize=axlabelfontsize, fontname=axlabelfontname)
  26. general.get_figure(show, r, figtype, 'pcaplot_3d', theme)