You want to use pyplot.grid:
x = numpy.arange(0, 1, 0.05)
y = numpy.power(x, 2)
fig = plt.figure()
ax = fig.gca()
ax.set_xticks(numpy.arange(0, 1, 0.1))
ax.set_yticks(numpy.arange(0, 1., 0.1))
plt.scatter(x, y)
plt.grid()
plt.show()
ax.xaxis.grid and ax.yaxis.grid can control grid lines properties.
To show a grid line on every tick, add
plt.grid(True)
For example:
import matplotlib.pyplot as plt
points = [
(0, 10),
(10, 20),
(20, 40),
(60, 100),
]
x = list(map(lambda x: x[0], points))
y = list(map(lambda x: x[1], points))
plt.scatter(x, y)
plt.grid(True)
plt.show()
In addition, you might want to customize the styling (e.g. solid line instead of dashed line), add:
plt.rc(‘grid’, linestyle=”-“, color=’black’)
For example:
import matplotlib.pyplot as plt
points = [
(0, 10),
(10, 20),
(20, 40),
(60, 100),
]
x = list(map(lambda x: x[0], points))
y = list(map(lambda x: x[1], points))
plt.rc(‘grid’, linestyle=”-“, color=’black’)
plt.scatter(x, y)
plt.grid(True)
plt.show()