Reputation: 3
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
But I want it to be like this
I tried to replace
plt.scatter(x, y)
by
plt.plot(x, y)
but I got something different
Upvotes: 0
Views: 80
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