5

I want to plot some y-data with an x-axis that does not appear in numerical order:

For example:

y = [100, 99, 93, 88, 85, 43]
x = [0, 5, 4, 3, 2, 1]
plot(x, y)

I'd like the x-axis to show up as 0, 5, 4, 3, 2, 1, in that order, evenly spaced, with the appropriate y values above.

How can I do this?

1 Answer 1

7

I'd use the x 'values' as the xtick labels instead. For example:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
y = [100, 99, 93, 88, 85, 43]
xt = [0, 5, 4, 3, 2, 1]
ax.plot(y)
ax.set_xticklabels(xt)
fig.savefig("out.png")

produces

pic with changed xtick labels

More generally, you could set the x coordinates to be whatever you wanted, and then set xticks and xticklabels appropriately.

Sign up to request clarification or add additional context in comments.

1 Comment

tested this on matplotlib 3.1.3 and you also need ax.set_xticks(xt) after ax.plot(y) for it to work correctly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.