Better way to chose numbers of x and y ticklabels in Python Matplotlib.pyplot


The result is:

Better way to chose numbers of x and y ticklabels in Python Matplotlib.pyplot

This page shows the better way to chose numbers of x and y ticklabels.

In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
In [2]:
x = [0,2,5,9,10]
y = [0,1,2,3,4.5]
Not good xticks
In [3]:
plt.plot(x,y,'o-')
plt.show()
Better xticks
In [4]:
plt.plot(x,y,'o-')
plt.xticks(x,x)
plt.yticks(y,y)
plt.show()
in "ax" style:
In [5]:
fig, ax = plt.subplots()
ax.plot(x,y,'o-')
ax.set_xticks(x)
ax.set_yticks(y)
plt.show()