1

So I'm trying to add a line of best fit to a graph using the plt.axhline() feature. The code I have currently is below which is working currently but doesn't include the axhline code,

df = pd.DataFrame(pd.read_csv('test5.csv', sep=','))
x = df["IE ratio"]
y = df["109"]
x1 = df["IE ratio"].mean()

plt.axvline(x1, 0, 1, c= 'k')
plt.scatter(x, y, s = 10)
plt.ylabel('Appearance of mutation')
plt.xlabel('IE spectrum')
plt.show()

I've tried to bring in the plt.axhline() feature but can't work out what I need to put in the bracket to get my desired output.

Here's the plot I get with a red line I've drawn on to show what I'm hoping to produce.

Outputted graph

Thanks in advance for any advice or help!

RJPython
  • 43
  • 6
  • [`axhline`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axhline.html) adds a horizontal (flat) line. Definitely not what you're looking for. – BigBen Apr 09 '21 at 16:23
  • Do you know of any other function or plot tool I could use? – RJPython Apr 09 '21 at 16:46
  • [Perhaps similar](https://stackoverflow.com/questions/11856206/multivariate-polynomial-best-fit-curve-in-python). – BigBen Apr 09 '21 at 16:52
  • 1
    Also take a look at [seaborn's regplot](https://seaborn.pydata.org/generated/seaborn.regplot.html) which does this out of the box. It also adds a confidence interval. – JohanC Apr 09 '21 at 17:09

2 Answers2

0

Computing the mean of 109 for a set of cuts of IE ratio and plotting it might get you a bit further but more information would be needed to give you more relevant advice.

import numpy as np
df['109_mean'] = pd.cut(df['IE ratio'], bins=np.arange(4.6,5.6,0.01))
df.plot('IE ratio', '109_mean')
Guillaume Ansanay-Alex
  • 1,212
  • 2
  • 9
  • 20
0

I managed to get it working using old practise notebooks I have. I used the below code.

import matplotlib as plt
from numpy.polynomial import Polynomial
x = df["IE ratio"]
y = df["44"]
xfit = np.linspace(1.3, 4.0, 30)
q = Polynomial.fit(x, y, deg=5)
plt.scatter(x, y, s = 10)
plt.plot(xfit, q(xfit), c='k')
x1 = df["IE ratio"].mean()
plt.axvline(x1, 0, 1, c= 'k')

Thank you for your advice guys and gals :)

RJPython
  • 43
  • 6