0

I am trying curve fitting in python, and am getting a strange error. I am not sure what is the origin of this error. I have this step when I plot at a step of 1 instead of 0.1. Can someone please point me out why do I get this error.

import numpy as np

from matplotlib import pyplot as plt

coeff = [0.6e-4, 0.48e-3, -0.29e-2, -0.164, -0.400, -0.472, -0.330, -0.057, 0.306, 0.689, 1.061, 1.406, 1.715, 1.986, 2.223, 2.432, 2.616]

Temp  = [5, 10, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300]

coeff = [0, 0, 0, -0.164, -0.400, -0.472, -0.330, -0.057, 0.306, 0.689, 1.061, 1.406, 1.715, 1.986, 2.223, 2.432, 2.616]

Temp  = [5, 10, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300]


legends = np.polyfit(Temp,coeff,4)[::-1]

T = np.arange(4,300,1)
y = [sum([float(a*x**y) for y,a in enumerate(legends)]) for x in T]
T_ = np.arange(4,300,0.1)
y_ = [sum([a*x**y for y,a in enumerate(legends)]) for x in T_]

plt.figure()
plt.plot(Temp,coeff,"ko")
plt.plot(T,y,"ro")
plt.plot(T_,y_,"go")

plt.show()
tmdavison
  • 64,360
  • 12
  • 187
  • 165
fznfire
  • 11
  • 2

2 Answers2

0

When I ran your code, I got the message

Traceback (most recent call last):
   File "test.py", line 26, in <module>
       plt.plot(T,y,"ro")
   File "/astromake/opt/casa/stable/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2458, in plot
       ret = ax.plot(*args, **kwargs)
   File "/astromake/opt/casa/stable/lib/python2.7/site-packages/matplotlib/axes.py", line 3848, in plot
       for line in self._get_lines(*args, **kwargs):
   File "/astromake/opt/casa/stable/lib/python2.7/site-packages/matplotlib/axes.py", line 323, in _grab_next_args
       for seg in self._plot_args(remaining, kwargs):
   File "/astromake/opt/casa/stable/lib/python2.7/site-packages/matplotlib/axes.py", line 300, in _plot_args
       x, y = self._xy_from_xy(x, y)
   File "/astromake/opt/casa/stable/lib/python2.7/site-packages/matplotlib/axes.py", line 240, in _xy_from_xy
       raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension

This tells you that your code fails in line 26, for the reason that T and y are not of the same type. This is true: T is a np.array, while y is a list. You can solve this problem by calling np.asarray() on y:

y = np.asarray([sum([float(a*x**y) for y,a in enumerate(legends)]) for x in T])

and

y_ = np.asarray([sum([a*x**y for y,a in enumerate(legends)]) for x in T_])

Just as an aside, I did a quick Google search and found this question, which basically gives the answer to your problem as well.

Community
  • 1
  • 1
Maaike
  • 122
  • 9
0

The problem is that you are reusing the y variable name (in your enumerate()) when assigning y and y_, so that by the time you come to plot, y is no longer of the same shape as x.

Try:

T = np.arange(4,300,1)
y = [sum([float(a*x**m) for m,a in enumerate(legends)]) for x in T] # note the change from y -> m

T_ = np.arange(4,300,0.1)
y_ = [sum([a*x**n for n,a in enumerate(legends)]) for x in T_] # note the change from y -> n
tmdavison
  • 64,360
  • 12
  • 187
  • 165