2

I've created random points and drawn a graphic with the plot and data. Then I'm saving this figure as a image.

How am I able to do the following?

  • Sort this random point list , lower coordinates to upper coordinates.

    Example list: [[1,3], [1,2], [1,1]]

    Target: [[1,1], [1,2], [1,3]]

  • And using the target list as an example:

    Index 0 = [1,1] , Index 1 = [1,2] etc.
    How can I do math between these indexes like this: Y0 - Y1/ X0 - X1

Here's my code:

import numpy as np
import matplotlib.pyplot as plt
import cv2

points = np.random.randint(0, 9, size=(18,2))
print(points)
plt.plot(points[:,0], points[:,1], '.',color='k')


plt.savefig("graphic.png",bbox_inches="tight")
result = cv2.imread("graphic.png")
cv2.imshow("Result",result)

enter image description here

Graphic drawn with random created points . I'm trying to sort this points lower Y to higher,Y . For example on this graphic you can see minimum coordinate is (1,0) and second minimum coordinate is (7,1) .

  • To sort use `sorted(lst)`, where `lst = [ [1,3], [1,2], [1,1] ]` – DarrylG Jun 15 '20 at 19:38
  • That `key` is completely unnecessary @darry – yatu Jun 15 '20 at 19:40
  • Can you share a minimal example @emily? Seems unclear what your asking – yatu Jun 15 '20 at 19:41
  • @yatu--you're right. I place the key automatically since I forget that sort automatically sort lists that way by default. – DarrylG Jun 15 '20 at 19:42
  • @yatu i edited for you – Emily Giovanni Jun 15 '20 at 19:49
  • To sort a numpy array see [Sorting a 2D numpy array by multiple axes](https://stackoverflow.com/questions/2706605/sorting-a-2d-numpy-array-by-multiple-axes). `points[np.lexsort(np.transpose(points)[::-1])]` – JohanC Jun 15 '20 at 21:49
  • Well, you need to use the result somehow. E.g. `sorted_points = points[np.lexsort(np.transpose(points)[::-1])]` and then `plt.plot(sorted_points[:,0], sorted_points[:,1], '-',color='r')` – JohanC Jun 15 '20 at 22:08
  • @JohanC It's working like lower x and lower y to higher x and y , but i need primarly lower y to higher y . – Emily Giovanni Jun 15 '20 at 22:08
  • Just leave out the `[::-1]`: `sorted_points = points[np.lexsort(np.transpose(points))]` – JohanC Jun 15 '20 at 22:09
  • @JohanC that's it . It is working. If you can write this as a answer i would like to give a accept vote . – Emily Giovanni Jun 15 '20 at 22:13
  • Well, stackoverflow considers this a duplicate. – JohanC Jun 16 '20 at 06:35
  • Does this answer your question? [Sorting a 2D numpy array by multiple axes](https://stackoverflow.com/questions/2706605/sorting-a-2d-numpy-array-by-multiple-axes) – JohanC Jun 16 '20 at 06:35

2 Answers2

0

Their is this function in python called sorted. It is an amazing thing you know, we all love it. It basically sorts any list you give it. It truly is a wonderfull tool and I use it a lot when coding. All you have to do is simple:

Input

MyList = [13,7,4,4]
sortedList = sorted(MyList))

Output:

[4, 4, 7, 13]

Just like that your list is sorted. It can even sort you list which contains sublist. Just change the variable MyList to your own list. Now what if we wanted to reverse it, that's simple just set reverse to true

MyList = [13,7,4,4]
sortedList = sorted(MyList,reverse=True)

And of course this works with strings Also there is another way of doing implementing the sort function just like this:

MyList = [13,7,4,4]
MyList.sort()

Hope this is helpful.

0

To sort a 2D list, use a lambda like this:

mylist = [[1,3], [1,2], [1,1], [0,9]]
l1 = sorted(mylist, key = lambda elem: elem[0]) 
l2 = sorted(mylist, key = lambda elem: elem[1])

The first one sorts after the first element in sublists, the second one sorts after the second. The output is different,

[[0, 9], [1, 3], [1, 2], [1, 1]]
[[1, 1], [1, 2], [1, 3], [0, 9]]
TeodorHH
  • 31
  • 2