Python Forum

Full Version: Fit np.polyfit to data points
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey! Trying to get polyfit working. I want to fit a 2nd order polynomial to some data points, but the code returns no plot. All x10, x11, y10, etc. values have been manually pointed out from data files and they should be correct as I've done scatter plots with them.

 #This is how all data points have been assigned
x10 = 79.3
x11 = 82.6
x12 = 64.1
y10 = data1[1]/1800
y11 = data1[2]/1800
y12 = data1[3]/1800

#... for the rest of the points

#For plotting:

xs1 = [x10, x20, x30, x40, x60]
ys1 = [y10, y20, y30, y40, y60]

xs2 = [x11, x21, x31, x41, x61]
ys2 = [y11, y21, y31, y41, y61]

xs3 = [x12, x22, x32, x42, x62]
ys3 = [y12, y22, y32, y42, y62]

print(np.poly1d(np.polyfit(xs1, ys1, 2)))
print(np.poly1d(np.polyfit(xs2, ys2, 2)))
print(np.poly1d(np.polyfit(xs3, ys3, 2)))

plt.plot(xs1, np.poly1d(np.polyfit(xs1, ys1, 2)), 
         label='0')
plt.plot(xs2, np.poly1d(np.polyfit(xs2, ys2, 2)), 
        label='1')
plt.plot(xs3, np.poly1d(np.polyfit(xs3, ys3, 2)), 
         label='2')
plt.legend()
plt.show()
The code simply doesn't return a plot, and instead shows a few individual data points. The output is this for the printed values:

       2
1.383 x - 332.7 x + 1.998e+04
       2
1.822 x - 440.9 x + 2.658e+04
       2
1.469 x - 329.9 x + 1.798e+04

Traceback (most recent call last):

  File "<ipython-input-20-3142fa5ba452>", line 1, in <module>
    runfile('C:/.../Python scripts/activity.py', wdir='C:/.../Python scripts')

  File "C:\...\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "C:\...\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/.../Python scripts/activity.py", line 137, in <module>
    label='0')

  File "C:\...\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 3240, in plot
    ret = ax.plot(*args, **kwargs)

  File "C:\...\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 1710, in inner
    return func(ax, *args, **kwargs)

  File "C:\...\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 1437, in plot
    for line in self._get_lines(*args, **kwargs):

  File "C:\...\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 404, in _grab_next_args
    for seg in self._plot_args(this, kwargs):

  File "C:\...\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 384, in _plot_args
    x, y = self._xy_from_xy(x, y)

  File "C:\...\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 243, in _xy_from_xy
    "have shapes {} and {}".format(x.shape, y.shape))

ValueError: x and y must have same first dimension, but have shapes (5,) and (3,)
.
You may need to call the poly1d perhaps
plt.plot(xs1, np.poly1d(np.polyfit(xs1, ys1, 2))(xs1), 
         label='0')
(Jul-07-2021, 06:36 AM)Gribouillis Wrote: [ -> ]You may need to call the poly1d perhaps
plt.plot(xs1, np.poly1d(np.polyfit(xs1, ys1, 2))(xs1), 
         label='0')

Ah, thanks! Now it plots points with no error, but not a curve. Is this normal?
Not sure about that. You may have a wrong default line style. Try plot(…,…, '-') or plot(…,…, linestyle='solid')