简体   繁体   中英

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?

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

图片带有更改的xtick标签

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM