简体   繁体   中英

Python scatter-plot: Conditions for marker styles?

I have a data set I wish to plot as scatter plot with matplotlib, and a vector the same size that categorizes and labels the data points (discretely, eg from 0 to 3). I want to use different markers for different labels (eg 'x' for 0, 'o' for 1 and so on). How can I solve this elegantly? I am quite sure I am just missing out on something, but didn't really find it, and my naive approaches failed so far...

What about iterating over all markers like this:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(100)
y = np.random.rand(100)
category = np.random.random_integers(0, 3, 100)

markers = ['s', 'o', 'h', '+']
for k, m in enumerate(markers):
    i = (category == k)
    plt.scatter(x[i], y[i], marker=m)

plt.show()

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