0

For understanding, I generated two 2D arrays. After that, I plot the scatter plot of that array. Now I want to draw a linear trendline and also the equation of that line. But I am doing something wrong. I really don't know how to get equation as well. My code is:

# Import Libraries
import numpy as np
import matplotlib.pyplot as plt

# Generate Random data 
a = np.zeros(shape=(8,8))
a[0] = [1,2,3,4,5,6,7,8]
a[1] = [1,0,3,4,0,6,6,8]
a[2] = [1,2,3,4,5,3,7,8]# Import Libraries
import numpy as np
import matplotlib.pyplot as plt

# Generate Random data 
a = np.zeros(shape=(8,8))
a[0] = [1,2,3,4,5,6,7,8]
a[1] = [1,0,3,4,0,6,6,8]
a[2] = [1,2,3,4,5,3,7,8]
a[3] = [1,2,3,0,5,6,7,8]
a[4] = [1,2,3,4,5,6,7,5]
a[5] = [1,2,3,4,5,6,7,8]
a[6] = [1,2,0,1,5,0,8,8]
a[7] = [1,2,3,4,5,6,7,8]

b = np.zeros(shape=(8,8))
b[0] = [1,1,3,4,5,6,7,8]
b[1] = [1,0,3,4,5,6,7,8]
b[2] = [1,2,3,4,5,6,5,6]
b[3] = [2,2,3,0,5,6,7,8]
b[4] = [1,2,3,8,8,6,7,8]
b[5] = [1,2,3,4,5,6,7,9]
b[6] = [1,2,6,4,5,0,7,8]
b[7] = [1,2,3,4,5,6,7,9]

# Draw scatterplot
plt.figure();
plt.title('Scatter plot')
plt.xlabel('a')
plt.ylabel('b')
plt.xlim(0, 10)
plt.ylim(0, 10)
plt.scatter(a, b)
plt.show()

# Add trendline with equation
#z = np.polyfit(a, b, 1)
#p = np.poly1d(z)
#plt.plot(a,p(a),"r--")
#print "y=%.6fx+(%.6f)"%(z[0],z[1]) #Dont know how it comes!

Thanks for help and suggestions!

SAMPHY86
  • 23
  • 1
  • 1
  • 7
  • @ImportanceOfBeingErnest I don't think so this question was asked before as you marked as a duplicate! Or may be I didn't search properly. As a suggestion, it is better to provide the link of the reply rather than just mark as duplicate. – SAMPHY86 Mar 25 '18 at 21:59
  • Not sure if I understand your comment. But the general question on how to add a trendline / fit to a matplotlib plot is sufficiently handled in the duplicate question. Since this question here does not state in how far other solutions do not help, it is surely a duplicate of the linked question. Please also read [ask] and [Duplicates](https://stackoverflow.com/help/duplicates). – ImportanceOfBeingErnest Mar 25 '18 at 22:05
  • @ImportanceOfBeingErnest Exactly, you didn't understand my comment as well as my question. I think you need to read both question and answer! if you want. Bottomline which I grabbed from the excellent answer of sacul for plotting a trendline from 2D numpy array is that you first need to convert it into 1D array using array.flatten(). Cheers, – SAMPHY86 Mar 26 '18 at 00:41

1 Answers1

9

The following code works:

plt.figure();
plt.suptitle('Scatter plot')
plt.xlabel('a')
plt.ylabel('b')
plt.scatter(a, b)

z = np.polyfit(a.flatten(), b.flatten(), 1)
p = np.poly1d(z)
plt.plot(a,p(a),"r--")
plt.title("y=%.6fx+%.6f"%(z[0],z[1])) 

plt.show()

enter image description here

np.polyfit, in your case, needs to have x and y as 1d arrays. I put the equation (y = coef x + b) as the title of the plot, but you can change that as you wish.

For instance, plt.text(8,1,"y=%.6fx+%.6f"%(z[0],z[1]), ha='right') instead of plt.title("y=%.6fx+%.6f"%(z[0],z[1])) would print your equation nicely in the lower right corner of your plot (right aligned, at the coordinates x=8, y=1)

sacuL
  • 49,704
  • 8
  • 81
  • 106