0

my code looks something like this:

%matplotlib inline 
import matplotlib.pyplot as plt
import numpy as np

for x in np.arange(0,100,5):

plt.scatter(x, x**2, c="blue", marker="o",s=1)


plt.legend()
plt.xlabel("$x$", fontsize=16)
plt.ylabel("$f(x)$", fontsize=16)
plt.show()

which gives me the attached picture: plot

How can I connect the points with lines, or even more preferably, a smooth curve?

Thanks for your help in advance!

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Morley
  • 25
  • 2
  • 4

2 Answers2

1

You can use plot to create a smooth line rather than scatter, which plots the individual points. Also, you do not need to do any loops here, matplotlib handles the plotting of arrays.

x = np.arange(0, 100, 5)

plt.plot(x, x ** 2, c="blue")

plt.xlabel("$x$", fontsize=16)
plt.ylabel("$f(x)$", fontsize=16)
plt.show()

Which gives:

enter image description here

Edit

I don't think there is a way to join the points directly using scatter. You could do

plt.scatter(x, x ** 2, c="blue",s=1)
plt.plot(x, x ** 2, c="blue")

which is essentially

plt.plot(x, x**2, color="blue", marker="o")

which is the same as @ImportanceOfBeingErnest's answer

DavidG
  • 24,279
  • 14
  • 89
  • 82
  • Ok, I realized my question was a bit misleading. I don't want to use plt, but I'm asking for a specific way to do it with scatter. – Morley Sep 27 '17 at 12:12
0

A scatter plot shows individual points of (potentially) different size and color. It seems that in this case, you want to have line plot.
Second, there is no reason to loop over the individual points from the array. Instead you want to supply the complete array to the plotting function.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,100,5)

plt.plot(x, x**2, color="blue", marker="o",ms=5, label="label")


plt.legend()
plt.xlabel("$x$", fontsize=16)
plt.ylabel("$f(x)$", fontsize=16)
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712