简体   繁体   中英

Unordered axes in scatter plot

I am trying to plot a cluster using scatter plot. I have an array of clusters colors and x and y arrays but when I am plotting them, the x and y axis are taking the input array order

I have checked out the other question that was similar but it doesn't seem to be the problem here. Frankly, I don't understand where the problem is.

data = [['11', '2'], ['1', '19'], ['2', '1'], ['17', '4'], ['5', '4'], ['7', '12'], ['14', '5'], ['19', '6'], ['19', '9'], ['6', '15'], ['6', '17'], ['6', '10'], ['3', '11'], ['14', '19'], ['10', '8'], ['9', '2'], ['3', '9'], ['7', '14'], ['14', '11'], ['13', '19'], ['1', '13'], ['9', '18'], ['6', '14'], ['9', '7'], ['15', '12'], ['14', '10'], ['1', '13'], ['19', '13'], ['12', '15'], ['16', '2'], ['18', '14'], ['9', '8'], ['13', '7'], ['14', '13'], ['6', '8'], ['16', '2'], ['19', '18'], ['8', '10'], ['6', '17'], ['4', '8'], ['14', '10'], ['15', '19'], ['7', '6'], ['4', '14'], ['7', '19'], ['10', '18'], ['20', '3'], ['5', '15'], ['8', '11'], ['18', '13'], ['8', '9'], ['20', '5'], ['10', '12'], ['17', '18'], ['6', '12'], ['5', '8'], ['17', '13'], ['10', '7'], ['16', '14'], ['4', '16'], ['12', '2'], ['14', '11'], ['16', '3'], ['11', '17'], ['14', '19'], ['14', '13'], ['17', '18'], ['10', '20'], ['15', '4'], ['10', '13'], ['8', '6'], ['2', '14'], ['18', '5'], ['10', '8'], ['4', '19'], ['12', '15'], ['10', '15'], ['7', '18'], ['8', '20'], ['11', '18'], ['13', '15'], ['10', '19'], ['6', '2'], ['14', '20'], ['6', '12'], ['3', '14'], ['7', '2'], ['19', '9'], ['18', '11'], ['9', '3'], ['13', '6'], ['11', '1'], ['10', '11'], ['16', '2'], ['16', '7'], ['9', '12'], ['1', '17'], ['5', '11'], ['11', '10'], ['4', '15']]
clusters = [1, 0, 0, 2, 0, 3, 4, 2, 0, 5, 5, 3, 6, 7, 8, 1, 6, 3, 9, 7, 6, 10, 3, 8, 9, 9, 6, 11, 12, 2,
11, 8, 4, 9, 3, 2, 0, 3, 5, 3, 9, 7, 8, 5, 5, 10, 0, 5, 3, 11, 3, 2, 3, 7, 3, 3, 9, 8, 9, 5, 1, 9, 2, 7, 7, 9, 7, 10, 2, 12, 8, 6, 2, 8, 5, 12, 12, 5, 10, 7, 12, 10, 0, 7, 3, 6, 1, 0, 11,
1, 4, 1, 3, 2, 4, 3, 0, 3, 8, 5]
x,y = zip(*data)
plt.scatter(np.array(x), np.array(y), c = clusters)
plt.show()

I assume the axis are supposed to stay ordered but they aren't

1

Just sort your axes arrays

sorted(list_var)

or

list_var.sort()

even numpy array has a sort() method

as @Guybrush said, you have your elements in data as strings, so you may try something like:

data = [[int(i[0]), int(i[1])] for i in data]

and then all the other code, which now will have the axis in sorted order:

x,y = zip(*(data))
clusters = [1, 0, 0, 2, 0, 3, 4, 2, 0, 5, 5, 3, 6, 7, 8, 1, 6, 3, 9, 7, 6, 10, 3, 8, 9, 9, 6, 11, 12, 2,
11, 8, 4, 9, 3, 2, 0, 3, 5, 3, 9, 7, 8, 5, 5, 10, 0, 5, 3, 11, 3, 2, 3, 7, 3, 3, 9, 8, 9, 5, 1, 9, 2, 7, 7, 9, 7, 10, 2, 12, 8, 6, 2, 8, 5, 12, 12, 5, 10, 7, 12, 10, 0, 7, 3, 6, 1, 0, 11,
1, 4, 1, 3, 2, 4, 3, 0, 3, 8, 5]
plt.scatter(np.array(x), np.array(y), c = clusters)
plt.show()

Hope this has helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM