如何在matplotlib/pylab中使X轴不自动对值进行排序? [英] How to make X axis in matplotlib/pylab to NOT sort automatically the values?
问题描述
无论何时绘制,X轴都会自动排序(例如,如果我输入值3、2、4,它将自动将X轴从较小到较大排序.
Whenever I plot, the X axis sorts automatically (for example, if i enter values 3, 2, 4, it will automatically sort the X axis from smaller to larger.
我该怎么做,以使轴保持输入值3、2、4的顺序
How can I do it so the axis remains with the order I input the values i.e 3, 2, 4
import pylab as pl
data = genfromtxt('myfile.dat')
pl.axis('auto')
pl.plot(data[:,1], data[:,0])
我找到了一个函数set_autoscalex_on(FALSE),但是我不确定如何使用它或它是否是我想要的. 谢谢
I found one function, set_autoscalex_on(FALSE) but I'm not sure how to use it or whether it is what I want. Thanks
推荐答案
您可以提供一个虚拟的x范围,然后覆盖xtick标签.我确实同意以上质疑是否是其最佳解决方案的评论,但是如果没有任何上下文,那就很难判断.
You could provide a dummy x-range, and then override the xtick labels. I do agree with the comments above questioning wether its the best solution, but thats hard to judge without any context.
如果您确实愿意,可以选择以下选项:
If you really want to, this might be an option:
fig, ax = plt.subplots(1,2, figsize=(10,4))
x = [2,4,3,6,1,7]
y = [1,2,3,4,5,6]
ax[0].plot(x, y)
ax[1].plot(np.arange(len(x)), y)
ax[1].set_xticklabels(x)
如果您使用日期,为什么不在轴上绘制实际日期(如果您确实希望在轴上设置29 30 1 2等,则可以按月的日期进行格式化?
edit: If you work with dates, why not plot the real date on the axis (and perhaps format it by the day-of-month if you do want 29 30 1 2 etc on the axis?
这篇关于如何在matplotlib/pylab中使X轴不自动对值进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!