1

How can scatter plot color be looped? my code:

col = {'Male':'green','Female':'blue'}

gender = [‘Male’,’Female’,’Male’,’Male’,’Female’, …]

Matched_Days = [list of days…]

Marital_Status = [list of statuses…]

for type in gender:

plt.scatter(Marital_Status, Matched_Days, c=col[type])

I only get one color: blue because last gender is ‘female’ in list.

For some reason, I can't get it to loop and register all colors inside the dictionary

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
Adam Schroeder
  • 748
  • 2
  • 9
  • 23

1 Answers1

1

You're not using matplotlib correctly. You only need one scatter, not a while loop.

gender = [‘Male’,’Female’,’Male’,’Male’,’Female’, …]
gender_color=[]
for elem in gender:
  if elem=="Male":
    gender_color.append("green")
  else:
    gender_color.append("blue")
Matched_Days = [list of days…] 
Marital_Status = [list of statuses…]   
plt.scatter(Marital_Status, Matched_Days, c=gender_color)
plt.show()

The c argument can take a list of colors. You shouldn't use a for loop unless you want multiple plots.

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44