简体   繁体   中英

Python group items in list1 based on items in list2

I have two lists one has points like

list1 = [1.8, 4.5, 1.1, 2.1, 9.8, 7.6, 11.32, 3.2, 0.5, 6.5] 

and the other has points ranging from 0-x based on what the user enters. Something along the lines of

list2 = [0, 1, 2, 0, 4, 4, 4, 3, 2, 1]

I am trying to graph them on a scatter plot based on their color using

plt.scatter(list1, list2)
plt.show()

That is graphing them how I want, but they are all the same color. I know that to switch the color I would do

plt.scatter(list1, list2, color="red")

but that changes every single point to red. I want to be able to say, for each point in list2, change the points color at the same index in list1, and then graph that.


import matplotlib.pyplot as plt    
list1 = [1.8, 4.5, 1.1, 2.1, 9.8, 7.6, 11.32, 3.2, 0.5, 6.5] 
list2 = [0, 1, 2, 0, 4, 4, 4, 3, 2, 1]
plt.scatter(list1, list2, color="red")
plt.show()

You can use an array as color:

import matplotlib.pyplot as plt

list1 = [1.8, 4.5, 1.1, 2.1, 9.8, 7.6, 11.32, 3.2, 0.5, 6.5]
list2 = [0, 1, 2, 0, 4, 4, 4, 3, 2, 1]
plt.scatter(list1, list2, c=list2)

output:

散色

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