How to change outliers to some other colors in a scatter plot

前端 未结 2 1848
耶瑟儿~
耶瑟儿~ 2020-12-06 15:12

If I have a scatter plot like this

I was wondering is there any way to change the obvious outliers, like the three on the top, to some other colors in the same plot?

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 15:44

    ImportanceOfBeingErnest has a great answer. Here's a one-liner I use if I have an array corresponding to enum categories for the data points (especially useful when visualizing data pre divided into classes).

    import numpy as np
    import matplotlib.pyplot as plt
    
    num = 1000
    x= np.random.rand(1,100)
    y= np.random.rand(1,100)*2
    
    # Creating a simple data point classification criteria, classes in this case will be 0, 1 and 2
    classes = np.round(y)
    
    # Passing in the classes for the "c" argument is super convinient
    plt.scatter(x,y, c=classes,cmap=plt.cm.Set1)
    plt.show()
    

    Corresponding scatter plot that divides the graph into 3 colored regions:

提交回复
热议问题