简体   繁体   English

Python 基于 list2 中的项目对 list1 中的项目进行分组

[英]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.另一个根据用户输入的内容从 0 到 x 不等。 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我正在尝试根据它们的颜色使用散点图 plot 绘制它们

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.我想说的是,对于 list2 中的每个点,更改 list1 中相同索引处的点颜色,然后绘制它。


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: output:

散色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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