Question

I have this code to save four plots into a figure:

plt.figure(figsize=(20,18))
for i in range(4):
    labels = [...]
    x = ...
    y = ...
    plt.subplot(221+i)
    plt.title('Title here')
    plt.xlabel('x-axis label')
    plt.ylabel('y-axis label')
    plt.plot(x, y, 'ro')
plt.savefig('fig.png', format='png')

Anyway I'd like to replace ticks on x-axis with labels. I tried plt.xticks(labels), but it doesn't work.

update I finally managed that thanks to solution given by Lev. The full code:

plt.figure(figsize=(20,18))
for i in range(4):
    labels = [...]
    x = ...
    y = ...
    plt.subplot(221+i)
    plt.title('Title here')
    plt.xlabel('x-axis label')
    plt.ylabel('y-axis label')
    plt.plot(x, y, 'ro')
    plt.xticks(range(len(x)), labels)
plt.savefig('fig.png', format='png')
Was it helpful?

Solution

As shown in the documentation, you can set the labels as follows:

plt.xticks(locations, labels)

If you don't want to change the tick locations, you can do:

plt.xticks(plt.xticks()[0], labels)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top