在matplotlib中指定连接顺序

时间:2013-10-14 22:25:28

标签: python matplotlib

目前,这些点按照它们输入图表的顺序加入。有没有办法从x坐标从左到右订购?

plt.errorbar(xvals, yvals, yerr=errors, linestyle='-', color='orange')

enter image description here

1 个答案:

答案 0 :(得分:1)

由于你有matplotlib你安装了numpy,你可以使用numpy按x顺序对x和y进行排序

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 8, .1)
np.random.shuffle(x)
y = np.sin(x)

sx = np.argsort(x)  # find the order for sorting x
x2 = x[sx]          #    apply this to x
y2 = y[sx]          #    apply this to y

plt.plot(x, y, 'y')
plt.plot(x2, y2, 'r', linewidth=4)

plt.show()

enter image description here