pythonscatter

Scatter plot in python


I have a vector X of size 100x2 and the corresponding binary labels in a vector y ={1, -1} of length 100. I would like to plot the scattered data with s.t. I get the features on the axis and the color of the data point corresponds to a label e.g. red is -1, yellow is 1 for a given data point.

I've been looking into matplotlib and the fcn scatter however it accepts only a single feature vector and its label.

I would be grateful for any help.


Solution

  • You can do this easily using seaborn (or matplotlib as well). Below is the code.

    1. I am creating a random array of size 100x2 and calling it X. I am creating a random array of 0s and 1s of size 100x1 and calling it Y
    >> import numpy as np
    >> X = np.random.randint(100, size=(100, 2))
    >> Y = np.random.choice([0, 1], size=(100))
    >> X
       array([[11, 47],
           [23,  2],
           [91, 14],
           [65, 32],
           [81, 78],
           ....
    >> Y
       array([0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1,
           0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0,
           1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1,
           0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1,
           1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1])
    

    Use Seaborn scatterplot

    import seaborn as sns
    sns.scatterplot(x=X[:,0], y=X[:,1], hue=Y)
    

    Output sns scatterplot

    enter image description here