Reputation: 311
Now I just want to plot a line graph based on two numpy arrays. My x and y are both two (150,1) arrays. After running the following code:
plt.plot(x,y)
What I get is: Line graph based on two numpy arrays
Hence I am so confused. What do those connected lines represent? I just want one line which goes through all of the points. Any help would be appreciated!
For the dataset, X is just a fixed (150,1) numpy array and y is computed based on the following polynomial function:
def PolyCoefficients(x, coeffs):
""" Returns a polynomial for ``x`` values for the ``coeffs`` provided.
The coefficients must be in ascending order (``x**0`` to ``x**o``).
"""
o = len(coeffs)
y = []
for i in range(len(x)):
value = 0
for j in range(o):
value += coeffs[j]*x[i]**j
y.append(value)
return y
The coefficients have been computed and what I want is just a line go through each point of (x,y)
Upvotes: 1
Views: 210
Reputation: 12417
The pairs of x
and y
represent the points on your graph. With plt.plot() you join the points with a line. If the array x
is not in order, what you have is a line that goes back and forward across the graph. To avoid this you should order the x
array, and the y
accordly. Try with:
new_x, new_y = zip(*sorted(zip(x, y)))
plt.plot(new_x,new_y)
Upvotes: 1