简体   繁体   English

基于第三列中的值的颜色散点图?

[英]Color scatter plot points based on a value in third column?

I am currently plotting a scatterplot based on two columns of data. 我目前正在根据两列数据绘制散点图。 However, I would like to color the datapoints based on a class label that I have in a third column. 但是,我想基于第三列中的类标签为数据点着色。

The labels in my third column are either 1,2 or 3. How would I color the scatter plot points based on the values in this third column? 我的第三列中的标签是1,2或3.我如何根据第三列中的值为散点图颜色着色?

plt.scatter(waterUsage['duration'],waterUsage['water_amount'])
plt.xlabel('Duration (seconds)')
plt.ylabel('Water (gallons)')

The scatter function happily takes a list of numbers representing color. scatter函数愉快地采用表示颜色的数字列表。 You can play with a colormap, too, if you want (but you don't have to): 如果需要,您也可以使用色彩映射(但您不需要):

plt.scatter(waterUsage['duration'], waterUsage['water_amount'],\
            c=waterUsage['third_column'], cmap=plt.cm.autumn)

add another entry to your dictionary "color" 在词典中添加另一个条目“color”

def addcolor(b):

    a=b
    for x in range(len(a['third_column'])):
        if a['third_column'][x]==1: a['color'][x]='rosybrown'
        elif a['third_column'][x]==2: a['color'][x]='papayawhip'
        elif a['third_column'][x]==3: a['color'][x]='chartreuse'
    return a

waterUsage = addcolor(waterUsage)

plt.scatter(waterUsage['duration'], 
            waterUsage['water_amount'],
            c=waterUsage['color'])

matplotlib accepts grayscale, rgb, hex, and html colors: matplotlib接受灰度,rgb,十六进制和html颜色:

http://matplotlib.org/api/colors_api.html http://matplotlib.org/api/colors_api.html

html color list, by group: html颜色列表,按组:

https://www.w3schools.com/colors/colors_groups.asp https://www.w3schools.com/colors/colors_groups.asp

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

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