0

While joining the points given by scatterplot in matplotlib, I only want to get the natural fit, and not x-y , which becomes a zig-zag as shown below.

How can I do this in matpotlib ?

PS: I don't want fitting polynomial/regression line, just the regular natural line

from pylab import *
import matplotlib.pyplot as plt

//Generate some x-y data for yourself...
x=[key for key in my_dict.keys()]
y=[imp for imp in my_dict.values()]
xlim([min(x),max(x)])
ylim([min(y),max(y)])
plt.scatter(x,y)

I get :

enter image description here

On doing basic plot along with this, I get connected, but overlapping lines

plt.plot(x, y, '-o')
plt.show()

enter image description here

What I would like to have:

enter image description here

Related q/a but doesn't exactly fit my case

Fallback Alternatives - Fit a n-th degrees polynomial as here - Multivariate (polynomial) best fit curve in python?

Edit :- I tried the code below as suggested

[x, y] = zip(*sorted(zip(x, y), key=lambda x: x[0])) ###
plt.plot(x, y, '-o') 

here's what I now get, butI am looking for something more smoother.

enter image description here

Community
  • 1
  • 1
ekta
  • 1,560
  • 3
  • 28
  • 57

1 Answers1

1

In order for plt.plot(x, y, '-o') to work, you will need to sort your data in x so that the line doesn't appear disjointed. You can do that with something like this:

[x, y] = zip(*sorted(zip(x, y), key=lambda x: x[0]))

That will sort both data, with x as the key.

pseudocubic
  • 1,039
  • 1
  • 14
  • 20
  • Not one of the best, but this works.I was looking for something "smoother" (I guess I will need polyfit there?).Edited my question with your suggestion & plot I now see .Thanks. – ekta Apr 10 '14 at 05:57
  • 1
    ekta, I think you did something wrong (or maybe the comment was wrong. I guess what pseudocubic was saying is that you need to sort x-values of your data and for each sorted x plot corresponding y(x). This operation should not change your scatter plot at all. All you are doing is that plot function connects the points one by one – Sleepyhead Apr 10 '14 at 06:14
  • I just tried pseudocubic solution and it worked perfectly for me. Try converting your data to numpy array - maybe that'll help. – Sleepyhead Apr 10 '14 at 06:23