Someone
Someone

Reputation: 3

How can I connect scatter points using matplotlib.pyplot

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

Upvotes: 0

Views: 80

Answers (1)

Heikura
Heikura

Reputation: 1089

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()

Upvotes: 1

Related Questions