OregonTrail
OregonTrail

Reputation: 9049

How to produce Matplotlib plot with x-axis out of order?

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?

Upvotes: 5

Views: 9324

Answers (1)

DSM
DSM

Reputation: 353549

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.

Upvotes: 7

Related Questions