Advertisement
OreganoHauch

violin plot (working example)

Dec 9th, 2020 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. # load the respective cached dictionary for the definition of "kmeansDic" throughout this project
  2. def load_cache_kmeans(silhouette_setting = True, sample_size = 300000, k_lowest = 2, k_highest = 3, kmeans_input = 1, data_processing = 1, normalize_samples = False):
  3. os.chdir("/asap3/petra3/gpfs/p06/2018/data/11005475/scratch_cc/Jan")
  4.  
  5. if silhouette_setting:
  6. silhouette_str = f"_{sample_size}-samplesize"
  7. else:
  8. silhouette_str = ""
  9.  
  10. fpath = "/asap3/petra3/gpfs/p06/2018/data/11005475/scratch_cc/Jan/Cache/"+ f"kmeansDic({k_lowest},{k_highest})_{kmeans_input}-kmeans-input_{normalize_samples!s}-normalize-samples_{data_processing}-data-processing_{silhouette_setting!s}-silhouette{silhouette_str}.h5"
  11. kmeansDic = h5py.File(fpath, "r")
  12.  
  13. return kmeansDic
  14.  
  15. def violin(kmeans_input=7,currents=False, dpi=None, savepng=False):
  16. start = time.time()
  17.  
  18. figsize = (30*0.75,15*0.75)
  19. fontsize = figsize[0]
  20. fig, ax = plt.subplots(figsize=figsize)
  21.  
  22. if currents:
  23. current_array1 = np.array(load_cache_currents()["current_array"]["185"]).ravel()[np.newaxis,:].T
  24. current_array2 = np.array(load_cache_currents()["current_array"]["183"]).ravel()[np.newaxis,:].T
  25. current_array3 = np.array(load_cache_currents()["current_array"]["181"]).ravel()[np.newaxis,:].T
  26. current_array4 = np.array(load_cache_currents()["current_array"]["182"]).ravel()[np.newaxis,:].T
  27. current_array5 = np.array(load_cache_currents()["current_array"]["186"]).ravel()[np.newaxis,:].T
  28. numpy_data = np.concatenate((current_array1,current_array2,current_array3,current_array4,current_array5),axis=1)
  29. df = pd.DataFrame(data=numpy_data, columns=["Scan 185","Scan 183","Scan 181","Scan 182","Scan 186"])
  30. ax = sn.violinplot(data=df, scale="count",inner="box")
  31. else:
  32. numpy_dataDic = {}
  33. for scan_index in [185,183,181,182,186]:
  34. current_array_scan = np.array(load_cache_currents()["current_array"][str(scan_index)])
  35. data = current_array_scan.ravel()[np.newaxis,:].T
  36. scan = np.repeat(scan_index,len(data))[np.newaxis,:].T
  37. group = np.array(load_cache_kmeans(kmeans_input = kmeans_input)["2"]["cluster_numbers"]).ravel()[np.newaxis,:].T
  38. numpy_dataDic[scan_index] = np.concatenate((data,scan,group),axis=1)
  39. #plt.axvline(x=scan_index-181.5, color="grey", lw=1)
  40. numpy_data = np.concatenate((numpy_dataDic[185],numpy_dataDic[183],numpy_dataDic[181],numpy_dataDic[182],numpy_dataDic[186]))
  41. df = pd.DataFrame(data=numpy_data, columns=["XBIC","Scan","Group"])
  42. ax = sn.violinplot(x = "Scan", y = "XBIC", hue = "Group", data = df, order=[185,183,181,182,186], palette = "viridis", split=True, linewidth = 0, scale = "count", inner = "box", ax = ax)
  43.  
  44. ax.set_xlabel("Scan", labelpad = 10, fontsize=9/5*fontsize, fontweight="bold")
  45. ax.set_ylabel("XBIC", labelpad = 10, fontsize=9/5*fontsize, fontweight="bold")
  46. ax.tick_params(labelsize=fontsize, length=4/5*fontsize)
  47.  
  48. # add minor ticks for XBIC
  49. ax.yaxis.set_minor_locator(MultipleLocator(10))
  50. ax.tick_params(which='minor', length=2/5*fontsize)
  51. plt.title("Violin plot", pad = 15, fontsize=round(9/5*fontsize), fontweight="bold")
  52. plt.grid(axis="y", which="minor",lw=0.25)
  53.  
  54. # add vertical lines
  55. for x in range(5):
  56. plt.axvline(x=x+0.5, color="grey", lw=1)
  57.  
  58. ax.xaxis.set_ticks([0,1,2,3,4])
  59. ax.xaxis.set_ticklabels([185,183,181,182,186], fontsize=round(9/5*fontsize))
  60.  
  61. plt.grid(axis="y")
  62.  
  63. ax.legend(labels=["Group 1", "Group 2"], prop={'size': fontsize})
  64.  
  65. plt.show()
  66.  
  67. end = time.time()
  68. print(f"Plotting of the violin plots took {str(round(end-start,2))} seconds.")
  69.  
  70. if savepng:
  71. now = datetime.now()
  72. dt_string = now.strftime("%d-%m-%Y_%H_%M_%S")
  73. if dpi is None:
  74. fig.savefig("savefig/violin_" + dt_string + ".png", dpi=fig.dpi, bbox_inches="tight")
  75. else:
  76. fig.savefig("savefig/violin_" + dt_string + ".png", dpi=dpi, bbox_inches="tight")
  77. violin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement