Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to space data on x axis
#1
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!!


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)
   
Larz60+ write May-06-2025, 06:58 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
I have added for you this time, Please use BBCode tags on future posts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 7,223 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  saving data from text file to CSV file in python having delimiter as space K11 1 3,381 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  from global space to local space Skaperen 4 3,379 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  Difference Between Figure Axis and Sub Plot Axis in MatplotLib JoeDainton123 2 3,641 Aug-21-2020, 10:17 PM
Last Post: JoeDainton123
  Get database used data space from pyodbc susja 1 3,197 Aug-14-2020, 02:01 PM
Last Post: susja

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020