Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def violin(kmeans_input=7,currents=False, title=True, data_processing=1, dpi=None, savepng=False):
- start = time.time()
- figsize = (30*0.75,15*0.75)
- fontsize = figsize[0]
- fig, ax = plt.subplots(figsize=figsize)
- scan_order = [185,183,181,182,186]
- xlabels = ["-200","-100","0","+100","+200"]
- if currents:
- if data_processing == 1:
- current_array1 = np.array(load_cache_currents()["pixelplotting_array"][scan_order.index(185)]).ravel()[np.newaxis,:].T
- current_array2 = np.array(load_cache_currents()["pixelplotting_array"][scan_order.index(183)]).ravel()[np.newaxis,:].T
- current_array3 = np.array(load_cache_currents()["pixelplotting_array"][scan_order.index(181)]).ravel()[np.newaxis,:].T
- current_array4 = np.array(load_cache_currents()["pixelplotting_array"][scan_order.index(182)]).ravel()[np.newaxis,:].T
- current_array5 = np.array(load_cache_currents()["pixelplotting_array"][scan_order.index(186)]).ravel()[np.newaxis,:].T
- if data_processing == 3:
- current_array1 = np.array(load_cache_currents()["pixelplotting_array_direct"][scan_order.index(185)]).ravel()[np.newaxis,:].T
- current_array1 = current_array1 - np.nanmin(current_array1)
- current_array2 = np.array(load_cache_currents()["pixelplotting_array_direct"][scan_order.index(183)]).ravel()[np.newaxis,:].T
- current_array2 = current_array2 - np.nanmin(current_array2)
- current_array3 = np.array(load_cache_currents()["pixelplotting_array_direct"][scan_order.index(181)]).ravel()[np.newaxis,:].T
- current_array3 = current_array3 - np.nanmin(current_array3)
- current_array4 = np.array(load_cache_currents()["pixelplotting_array_direct"][scan_order.index(182)]).ravel()[np.newaxis,:].T
- current_array4 = current_array4 - np.nanmin(current_array4)
- current_array5 = np.array(load_cache_currents()["pixelplotting_array_direct"][scan_order.index(186)]).ravel()[np.newaxis,:].T
- current_array5 = current_array5 - np.nanmin(current_array5
- numpy_data = np.concatenate((current_array1,current_array2,current_array3,current_array4,current_array5),axis=1)
- df = pd.DataFrame(data=numpy_data, columns=xlabels)
- ax = sn.violinplot(data=df, scale="count",inner="box")
- else:
- numpy_dataDic = {}
- for scan_index in scan_order:
- current_array_scan = np.array(load_cache_currents()["pixelplotting_array"][scan_order.index(scan_index)])
- data = current_array_scan.ravel()[np.newaxis,:].T
- scan = np.repeat(scan_index,len(data))[np.newaxis,:].T
- group = np.array(load_cache_kmeans(kmeans_input = kmeans_input)["2"]["cluster_numbers"]).ravel()[np.newaxis,:].T
- numpy_dataDic[scan_index] = np.concatenate((data,scan,group),axis=1)
- #plt.axvline(x=scan_index-181.5, color="grey", lw=1)
- numpy_data = np.concatenate((numpy_dataDic[185],numpy_dataDic[183],numpy_dataDic[181],numpy_dataDic[182],numpy_dataDic[186]))
- df = pd.DataFrame(data=numpy_data, columns=["XBIC","Bias voltage (mV)","Group"])
- df = df.replace(1.0,"Group 1")
- df = df.replace(2.0,"Group 2")
- ax = sn.violinplot(x = "Bias voltage (mV)", y = "XBIC", hue = "Group", data = df, order=[185,183,181,182,186], palette = "viridis", split=True, linewidth = 0, scale = "count", inner = "box", ax = ax)
- ax.set_xlabel("Bias voltage (mV)", labelpad = 10, fontsize=9/5*fontsize, fontweight="bold")
- ax.set_ylabel("XBIC", labelpad = 10, fontsize=9/5*fontsize, fontweight="bold")
- ax.tick_params(labelsize=fontsize, length=4/5*fontsize)
- ax.xaxis.set_ticks([0,1,2,3,4])
- ax.xaxis.set_ticklabels(xlabels, fontsize=fontsize)
- # add minor ticks for XBIC
- ax.yaxis.set_minor_locator(MultipleLocator(10))
- ax.tick_params(which='minor', length=2/5*fontsize)
- plt.grid(axis="y", which="minor",lw=0.25)
- plt.grid(axis="y")
- if title:
- plt.title("Current distribution", pad = 15, fontsize=round(9/5*fontsize), fontweight="bold")
- # add vertical lines
- for x in range(5):
- plt.axvline(x=x+0.5, color="grey", lw=1)
- # meta = {
- # "colour": {"a": "red", "b": "blue"},
- # "label": {"a": "this a", "b": "this b"},
- # }
- # handles = [
- # Patch(
- # facecolor=meta["colour"][x],
- # label=meta["label"][x],
- # )
- # for x in meta["colour"].keys()
- # ]
- # fig.legend(
- # handles=handles,
- # ncol=2,
- # loc="upper right",
- # bbox_to_anchor=(0.5, 0.5),
- # fontsize=10,
- # handlelength=1,
- # handleheight=1,
- # frameon=False,
- # )
- ax.legend(prop={'size': fontsize})
- #ax.get_legend().remove()
- plt.show()
- end = time.time()
- print(f"Plotting of the violin plots took {str(round(end-start,2))} seconds.")
- if savepng:
- now = datetime.now()
- dt_string = now.strftime("%d-%m-%Y_%H_%M_%S")
- if dpi is None:
- fig.savefig("savefig/violin_" + dt_string + ".png", dpi=fig.dpi, bbox_inches="tight")
- else:
- fig.savefig("savefig/violin_" + dt_string + ".png", dpi=dpi, bbox_inches="tight")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement