简体   繁体   中英

Scatterplot with labeled data, marker in relation to label

i want to scatterplot a dataset with labeled data. I want the several classes to be displayed in different marker styles. For Data generation i use the command:

from sklearn.datasets import make_moons
X, y = make_moons(n_samples = 100, noise = 0.15)

After the data is generated:

X = array([[ 0.83193416,  0.67054039],
       [ 1.4017985 , -0.34708943],
       ...
       [ 1.02640652, -0.58107469],
       [-1.08443914,  0.51960219]])

y = array([0, 1, 1, 1, 0, ... 1, 0])

Equal sized Arrays have been generated, y is the label for data in X. Data labeled with 0 should be displayed as circles and data labeled with 1 should be displayed as triangles. Here is an example of what it has to look like: Example

Thanks in advance.

Use y as a selector to filter rows of X. The x and y vectors for the plot are in the *columns of X, so we must transpose the data for the plot to make sense. The splattering star * is used to split the columns into the first two arguments for plot(). The colors and shapes are in the final parameters 'bo' and 'g^':

import matplotlib.pyplot as plt

plt.plot(*X[y==0].T, 'bo')  # blue circles (replace o -> s for squares like the picture)
plt.plot(*X[y==1].T, 'g^')  # green triangles
plt.axis('equal')
plt.grid('on')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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