Home > Blockchain >  Python group items in list1 based on items in list2
Python group items in list1 based on items in list2

Time:10-09

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()

CodePudding user response:

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:

scatter color

  • Related