packages = ["matplotlib", "numpy", "pandas"]

Matplotlib Plot mit PyScript

Warten bis der python code durchgerechnet ist.
import pandas as pd #import scipy.stats as stats import matplotlib.pyplot as plt #import seaborn as sns # read Mastertable and Legend, two sheets in the same Excel Document namPaper = "example" xlsx_pathMaster = namPaper + ".xlsx" xlsx_pathLegend = "Excel/mastertable_merged_" + namPaper + ".xlsx" df = pd.read_excel(xlsx_pathMaster, sheet_name="Master", header=0) legend = pd.read_excel(xlsx_pathMaster, sheet_name="Legend", header=0) # add description from the legend to the dataframe as a new column for column in legend["name"].unique(): mapping = legend[legend["name"] == column].set_index("code")["meaning"].to_dict() df[column + "_label"] = df[column].map(mapping) df.copy() # method to be used for differentiating the p-value of t-test def get_sign_star_new(p): if p < 0.01: #p = round(p, 3) return rf"$\it{{p = {p:.4f}}}$" if p < 0.05: return rf"$\it{{p = {p:.3f}}}$" #p = round(p, 2) #return "P= " + str(p) else: return rf"$\it{{p = {p:.2f}}}$" targ = "" def createPlot(targ, id, scale_x=0.7): # key parameters from demanded column, colum name == variable targ summary = df.groupby(id)[targ].agg(mean="mean", std="std", n="count").reset_index() summary[id + "_label"] = summary[id].map(df.set_index(id)[id + "_label"].to_dict()) summary["sem"] = summary["std"] / summary["n"] ** 0.5 selected_groups = df[id + "_label"].unique() # needed? #df_filtered = df_filtered[df_filtered[id + "_label"].isin(selected_groups)] summary = summary.set_index(id + "_label").loc[selected_groups].reset_index() summary_text = summary.to_string(index=False) highest = 0 fig, ax = plt.subplots(figsize=(6, 5)) # draw mean +/- standarddeviation into the graph for i, row in summary.iterrows(): mean = row["mean"] std = row["std"] xpos = scale_x * i if (i == 0): ax.hlines(y=mean, xmin=i - 0.1, xmax=i + 0.1, linewidth=2, color="black") ax.vlines(x=i, ymin=mean - std, ymax=mean + std, linewidth=1, color="black") ax.hlines([mean - std, mean + std], xmin=i - 0.05, xmax=i + 0.05, linewidth=2, color="black") else: ax.hlines(y=mean, xmin=xpos*i - 0.1*xpos, xmax=xpos*i + 0.1*xpos, linewidth=2, color="black") ax.vlines(x=xpos*i, ymin=mean - std, ymax=mean + std, linewidth=1, color="black") ax.hlines([mean - std, mean + std], xmin=xpos*i - 0.05*xpos, xmax=xpos*i + 0.05*xpos, linewidth=2, color="black") ax.set_xticks([scale_x * i for i in range(len(summary))]) ax.set_xticklabels(summary[id + "_label"]) ax.set_ylabel(targ) # sns.stripplot( # #x=id + "_label", y=targ, data=df_filtered, hue="color", palette=color_map, ax=ax, alpha=0.7, jitter=0.15, # x=id + "_label", y=targ, data=df, hue = id+ "_label", ax=ax, alpha=0.7, jitter=0.15, # order=selected_groups # ) #ax.legend_.remove() for coll in ax.collections: offsets = coll.get_offsets() offsets[:, 0] = offsets[:, 0] * scale_x coll.set_offsets(offsets) all_values = list(df[targ].dropna().values) for i, row in summary.iterrows(): all_values.extend([row["mean"] - row["std"], row["mean"] + row["std"]]) # automatic ymin, ymax = min(all_values), max(all_values) padding = 0.1 * (ymax - ymin) # change for view on graph # maybe adjust for y-axis #ax.set_ylim(ymin - padding, ymax + (2 * padding)) if (ymin != 0): ax.set_ylim(ymin - padding, ymax + (2 * padding)) else: ax.set_ylim(ymin, ymax + (2 * padding)) ax.set_xlim(-0.2, len(summary) - 0.7) groups = [df[df[id + "_label"] == g][targ].dropna() for g in selected_groups] comparisons = [] if len(groups) == 2: text = "t-Test" # t-Test # tstat, pval = stats.ttest_ind(groups[0], groups[1], equal_var=False) # this is where we apply t-test (used for 2 groups) # print(f"\nt-Test: p-value = {pval:.4f}") # comparisons = [(selected_groups[0], selected_groups[1], pval)] # bars of significance y_min, y_max = ax.get_ylim() h = (y_max - y_min) * 0.05 # Höhe der Signifikanzbalken text_pval = "" # create stars for p-value # for i, (g1, g2, pval_p) in enumerate(comparisons): # stars = get_sign_star_new(pval_p) # text_pval += f"{g1} vs {g2},p={pval_p} \n" # if stars == "ns": # continue # x1 = list(selected_groups).index(g1) # x2 = list(selected_groups).index(g2) # y = 0.85*y_max - (i * h) # ax.text(((x1*scale_x) + (x2*scale_x)) / 2, y, stars, ha="center", va="bottom", fontsize=12) # textus = text + f": p = {pval:.3f}" # final_text = textus + "\n" + text_pval + "\n\n" + summary_text # ax.text( # 0, -0.25, final_text, # ha="left", va="top", fontsize=8, family="monospace", transform=ax.transAxes # ) filename = F"{namPaper}_plot{targ}.png" plt.tight_layout() plt.savefig(filename, dpi=300, bbox_inches="tight", pad_inches=0.5) plt.show() for targ in ["maximal glucose uptake", "maximal fatty acid uptake"]: createPlot(targ, "group")

Dieses Beispiel zeigt, wie Matplotlib-Diagramme direkt im Browser mit PyScript gerendert werden können.