Python matplotlib in the wrong order

Basically, I have two arrays, one of which contains the values ​​of the x axis, and the second contains the values ​​of the y axis. The problem is that when I do

plt.semilogy(out_samp,error_mc) 

I get it

enter image description here

It makes no sense. This is because the plot functions build everything as it occurs in the x array, without worrying about whether it is sorted in ascending order or not. How can I sort these two arrays so that the x array is sorted in ascending order of magnitude, and the y axis is sorted the same way so that the points are the same, but the graph is connected so that it does not make this mess?

Thank you in advance!

+8
source share
6 answers

Sort by x-axis value before plotting. Here is the MWE.

 import itertools x = [3, 5, 6, 1, 2] y = [6, 7, 8, 9, 10] lists = sorted(itertools.izip(*[x, y])) new_x, new_y = list(itertools.izip(*lists)) # import operator # new_x = map(operator.itemgetter(0), lists) # [1, 2, 3, 5, 6] # new_y = map(operator.itemgetter(1), lists) # [9, 10, 6, 7, 8] # Plot import matplotlib.pylab as plt plt.plot(new_x, new_y) plt.show() 

For small data, zip enough (as mentioned by other responders).

 new_x, new_y = zip(*sorted(zip(x, y))) 

Result,

enter image description here

+5
source

It is easier to zip , sort and un zip two data lists.

Example:

 xs = [...] ys = [...] xs, ys = zip(*sorted(zip(xs, ys))) plot(xs, ys) 

See the zip documentation here: https://docs.python.org/3.5/library/functions.html#zip

+3
source

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

enter image description here

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

enter image description here

+2
source

You can convert arrays to numpy arrays, then use argsort for the first array, take an array and sort both arrays with argsort array.

0
source

just do it

 list=zip(*sorted(zip(*(x,y)))) plt.plot(*list) 

the sorted function sorts according to the 1st argument ie the value of x

0
source

I think you need to sort one array, and the other array should also be sorted based on the first array. I got this solution from another stack overflow question. Most likely, this should be your decision.

 out_samp,error_mc=zip(*sorted(zip(out_samp,error_mc))) 

Now sketch these two values, you will get the correct graph.

0
source

All Articles