0

How can I connect scatter points using matplotlib.pyplot this is my code

x = data
y = pdf
plt.scatter(x, y)
plt.show

I am getting this plot

enter image description here

But I want it to be like this

https://i.ibb.co/zHhzZ35/correct.png

I tried to replace

plt.scatter(x, y)

by

plt.plot(x, y)

but I got something different

enter image description here

1 Answer 1

1

In order to get that correctly, I think you should sort the lists. You could do it like this:

# List indexes sorted
sorted_list = sorted(range(len(x)), key=lambda a: x[a])

# Based on sorted indexes, sort lists
x = [x[i] for i in sorted_list]
y = [y[i] for i in sorted_list]

# Plot
plt.plot(x,y)
plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

Note that with numpy you could use np.argsort instead of implementing it with list comprehension, and it's probably faster
Yeah that is true. :)

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.