0

I am trying to plot and linear fit the scatter (suppose 20) points. The plan is like this,

  1. take data from 20 csv files.
  2. scatter plot with label and legend(according to the file name).
  3. linear fit.
  4. perpendicular distance from the scatter point to the line.

I have done first two using this piece of code.

a_p=np.genfromtxt("a/stat.csv", delimiter=",", dtype=float)
a_r=np.genfromtxt("a/dis.csv", delimiter=",", dtype=float)

b_p=np.genfromtxt("b/stat.csv", delimiter=",", dtype=float)
b_r=np.genfromtxt("b/dis.csv", delimiter=" ", dtype=float)

plt.scatter(a_p[0]/a_p[8],(a_r[0]/a_r[2])**n, label="a")
plt.scatter(b_p[0]/b_p[8],(b_r[0]/b_r[2])**n, label="b")
#plt.plot(np.unique(x), np.poly1d(np.polyfit(x, y, 1))(np.unique(x)))

plt.legend()

Though there are many examples for linear fitting like this here, but they do declare two arrays x and y containing the points. But even if I store like those arrays and do the plot, I can't give customized legends.

EDIT:1 I have modified the code where, I have taken two arrays, x and y. Those arrays contain the points required for plot. However, getting wrong type of plot.

x=np.array([a_p[5]/a_p[2],b_p[5]/b_p[2],c_p[5]/c_p[2]],dtype=float)
y=np.array([(a_r[1]/a_r[2])**n,(b_r[1]/b_r[2])**n,(c_r[1]/c_r[2])**n],dtype=float)
plt.scatter(x,y)
m,b=np.polyfit(x,y,1)
plt.plot(x, y, x, np.array(x) * m+b)
plt.legend()

The plot is this,

my plot

Problem with the plot is, It is not scatter. Can I manually put legends to the scatter points by this method?

  • 2
    You can create two new arrays and generate the line via the method of the linked post. You draw the scatter plots like you already did, from the old arrays. – JohanC Sep 03 '21 at 13:55

0 Answers0