How to make matplotlib show all x coordinates?
Question:
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
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
?
Answers:
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.
Setting xticks using the values that are not evenly spaced makes a "bad" plot sometimes. For example, in the following case:
x = [1,2.9,3.1,4,5,6,7,8,9,10]
y = np.random.rand(10)
plt.scatter(x, y)
plt.xticks(x);
the x-tick labels are unreadable at some points.
It’s better if the tick labels are evenly spaced:
plt.scatter(x, y)
plt.xticks(range(min(x), max(x)+1));
FYI, you can get the limits of an axis by calling plt.xlim()
or plt.ylim()
so that you can set more accurate tick labels. Also, with plt.xticks()
(and plt.yticks()
), you can set arbitrary tick labels. For example, instead of numbers, tick labels could be strings (e.g. a
, b
, c
).
plt.scatter(x, y)
plt.xticks(range(min(x), max(x)+1));
ymin, ymax = plt.ylim() # (0.19667230980587072, 1.0097016485006163)
plt.yticks(np.linspace(0, 1, 4), ['0', 'a', 'b', 'c']);
plt.ylim(ymin, ymax);
The very same graph can be plotted by explicitly creating subplots and setting the properties of the Axes object as well.
fig, ax = plt.subplots(figsize=(5,2))
ax.scatter(x, y)
ax.set(xticks=np.arange(min(x), max(x)+1), yticks=np.linspace(0, 1, 4), yticklabels=['0', 'a', 'b', 'c'], ylim=ax.get_ylim());
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
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
?
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.
Setting xticks using the values that are not evenly spaced makes a "bad" plot sometimes. For example, in the following case:
x = [1,2.9,3.1,4,5,6,7,8,9,10]
y = np.random.rand(10)
plt.scatter(x, y)
plt.xticks(x);
the x-tick labels are unreadable at some points.
It’s better if the tick labels are evenly spaced:
plt.scatter(x, y)
plt.xticks(range(min(x), max(x)+1));
FYI, you can get the limits of an axis by calling plt.xlim()
or plt.ylim()
so that you can set more accurate tick labels. Also, with plt.xticks()
(and plt.yticks()
), you can set arbitrary tick labels. For example, instead of numbers, tick labels could be strings (e.g. a
, b
, c
).
plt.scatter(x, y)
plt.xticks(range(min(x), max(x)+1));
ymin, ymax = plt.ylim() # (0.19667230980587072, 1.0097016485006163)
plt.yticks(np.linspace(0, 1, 4), ['0', 'a', 'b', 'c']);
plt.ylim(ymin, ymax);
The very same graph can be plotted by explicitly creating subplots and setting the properties of the Axes object as well.
fig, ax = plt.subplots(figsize=(5,2))
ax.scatter(x, y)
ax.set(xticks=np.arange(min(x), max(x)+1), yticks=np.linspace(0, 1, 4), yticklabels=['0', 'a', 'b', 'c'], ylim=ax.get_ylim());