An alternative to sorting lists would be to use NumPy arrays and use np.sort() to sort. The advantage of using arrays would be a vectorized operation when computing a function such as y = f (x). The following is an example of constructing a normal distribution:
Without using sorted data
mu, sigma = 0, 0.1 x = np.random.normal(mu, sigma, 200) f = 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (x - mu)**2 / (2 * sigma**2) ) plt.plot(x,f, '-bo', ms = 2)
Output 1

Using np.sort () This allows you to directly use the sorted array x in calculating the normal distribution.
mu, sigma = 0, 0.1 x = np.sort(np.random.normal(mu, sigma, 200)) # or use x = np.random.normal(mu, sigma, 200).sort() f = 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (x - mu)**2 / (2 * sigma**2) ) plt.plot(x,f, '-bo', ms = 2)
Alternatively, if you already have x and y numpy.argsort , you can use numpy.argsort to sort them posteriori
mu, sigma = 0, 0.1 x = np.random.normal(mu, sigma, 200) f = 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (x - mu)**2 / (2 * sigma**2) ) plt.plot(np.sort(x), f[np.argsort(x)], '-bo', ms = 2)
In both cases, exit
Output 2
