1

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)

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. But actually my x is fixed and y is just the output from a polynomial function based on x. The coefficients have already been computed... Does the order of these two arrays change during the computation?
If you have just these 2 lists, order them for the graph. I've added some code to the answer, try it and see if it works
You are welcome, dont forget to acecpt the answer :) meta.stackexchange.com/questions/5234/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.