Search code examples
pythonmatplotlibscatter-plot

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, e.g. from 0 to 3). I want to use different markers for different labels (e.g. '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...


Solution

  • 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()