0

Currently the points are being joined in the order they are entered into the graph. Is there a way to order from left to right from the x co-ordinate?

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

enter image description here

Ollie
  • 544
  • 4
  • 22
  • 3
    You need to sort the `xvals` and `yvals` first. see http://stackoverflow.com/a/1903997/2530083 for how to sort 2 lists/arrays – rtrwalker Oct 14 '13 at 22:33

1 Answers1

1

Since you have matplotlib you have numpy installed, and you can use numpy to sort both x and y by the x-order.

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

tom10
  • 67,082
  • 10
  • 127
  • 137