2021年4月23日星期五

Problem finding proper angle (in degrees) for trendline created in matplotlib using polyfit

If someone could please explain/show how to get the proper angle read... In my example, the Angle should be between 40-45 degrees but I only show 5.71 degrees or just over 90 degrees (if I try reverse x/y). I have searched for awhile which has got me this far but I don't see how to fix this.. Help would be appreciated. Thank you in advance.

import pandas as pd  import numpy as np  import matplotlib.pyplot as plt    x = [1,2,3,4,5,6,7,8,9]  y = [1.1,1.4,1.3,1.6,1.1,1.6,1.7,2.2,1.9]    plt.plot(x,y, marker='o', markersize=1, color="green")  z = np.polyfit(x, y, 1)    p = np.poly1d(z)  plt.plot(x,p(x),"r--")    degree = np.rad2deg(np.arctan2(y[-1] - y[0], x[-1] - x[0]))  degree2 = np.rad2deg(np.arctan2(x[-1] - x[0], y[-1] - y[0]))  print(degree, degree2)  plt.show()  
https://stackoverflow.com/questions/67237852/problem-finding-proper-angle-in-degrees-for-trendline-created-in-matplotlib-us April 24, 2021 at 07:20AM

没有评论:

发表评论