May-05-2025, 12:55 PM
Hi everyone!
I'm trying to represent some data with matplotlib. For this particular set, I need to compare only two data, each composed of a mean + error bars. Problem is, as there is only two x "points", they are represented at each ends of the x axis, and I can't find how to position them better (see attachment for graph).
Do you know how I could better center the x points?
NB: I'm a doctoral student in biology and barely know coding, so please excuse the messy code.
Thank you in advance!!
I'm trying to represent some data with matplotlib. For this particular set, I need to compare only two data, each composed of a mean + error bars. Problem is, as there is only two x "points", they are represented at each ends of the x axis, and I can't find how to position them better (see attachment for graph).
Do you know how I could better center the x points?
NB: I'm a doctoral student in biology and barely know coding, so please excuse the messy code.
Thank you in advance!!
import matplotlib.pyplot as plt import numpy as np #upper, lower, estimate MKPU=[7.05E-07,3.85E-07,5.37E-07] MKPUR175=[1.77E-06,1.03E-06,1.37E-06] data=[MKPU,MKPUR175] mut_rates=[MKPU[2],MKPUR175[2]] x=[1,2] errors_low=[MKPU[1],MKPUR175[1]] errors_up=[MKPU[0],MKPUR175[0]] errors_low_calc=[] errors_up_calc=[] for i in range(len(errors_low)): errors_low_calc.append(mut_rates[i]-errors_low[i]) for j in range(len(errors_low)): errors_up_calc.append(errors_up[j]-mut_rates[j]) errors_calc=[errors_low_calc,errors_up_calc] pvalue=[1.00E+00,5.7627E-06] def convert_pvalue_to_asterisks(pvalue): if pvalue <= 0.001: return "***" elif pvalue <= 0.01: return "**" elif pvalue <= 0.05: return "*" return "ns" asterisks=[] for j in range(0,len(pvalue)): a=convert_pvalue_to_asterisks(pvalue[j]) asterisks.append(a) plt.figure(figsize=(3,5)) plt.errorbar(x,mut_rates,yerr=errors_calc,fmt='or',capsize=5,ecolor='black') #fmt: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot plt.xticks(x,('MKPU','MKPUR175')) for i in range(1,len(asterisks)): x1, x2 = 1, i+1 y, h, col = max(map(max,data)) + 1E-07, 6E-08*i, 'k' plt.plot([x1, x1, x2, x2], [y, y+h, y+h, y], lw=1.5, c=col) plt.text((x1+x2)*.5, y+h, asterisks[i], ha='center', va='bottom', color=col) plt.xlabel('p53 alleles',fontsize=15) plt.ylabel('Inactivation frequency',fontsize=15) plt.title('p53 CD alleles inactivation frequency',fontsize=18)[attachment=3301]