lordy
lordy

Reputation: 630

Why does "xticks" work on the figure but "set_xticks" not on the axis?

This should work but it does not?

plt.figure()
plt.plot(x)
plt.xticks(range(4), [2, 64, 77, 89])  # works

f, ax = plt.subplots(nrows=2, ncols=2)
ax[0, 0].plot(x)
ax[0, 0].set_xticks(range(4), [2, 64, 77, 89])  # does not work

Upvotes: 5

Views: 5174

Answers (1)

DavidG
DavidG

Reputation: 25363

When using the object orientated API (as in your second example), the functions to set the tick locations and the tick labels are separate - ax.set_xticks and ax.set_xticklabels. Therefore, you will need:

f, ax = plt.subplots(nrows=2, ncols=2)
ax[0, 0].plot(x)
ax[0, 0].set_xticks(range(4))
ax[0, 0].set_xticklabels([2, 64, 77, 89])

Upvotes: 5

Related Questions