循环散点图颜色

Looping scatter plot colors

散点图颜色如何循环? 我的代码:

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

我只有一种颜色:蓝色,因为列表中最后一个性别是“女性”。

出于某种原因,我无法让它循环并注册字典中的所有颜色

您没有正确使用 matplotlib。您只需要一个散点图,而不是一个 while 循环。

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

c 参数可以采用颜色列表。除非你想要多个图,否则你不应该使用 for 循环。