pythonmatplotlibplotaxis-labelsxticks

How to make matplotlib show all x coordinates?


For example in the following code:

import numpy as np
import matplotlib.pyplot as plt

N = 10
x = [1,2,3,4,5,6,7,8,9,10]
y = np.random.rand(N)

plt.scatter(x, y)
plt.show()

I get the following plot

enter image description here

as you can see, in the x axis, only the even values appear. How to force matplotlib to show all values, that is 1 2 3 4 5 6 7 8 9 10?


Solution

  • Use plt.xticks(x). See the documentation.

    Note that using only the input values for ticks will usually be confusing to a viewer. It makes more sense to use evenly spaced ticks.