How to use different marker for different point in scatter plot pylab

后端 未结 1 1546
小蘑菇
小蘑菇 2020-12-11 17:01

I want to use the scatter plot function of pylab

x = [1,2,3,4,5]
y = [2,1,3,6,7]

there are two clusters in this 5 points, index 1-2(cluster

相关标签:
1条回答
  • 2020-12-11 17:25

    To achieve this result you need to call scatter multiple times on the same axis. The good news is you can automate this for your given data:

    import matplotlib.pyplot as plt
    
    x = [1,2,3,4,5]
    y = [2,1,3,6,7]
    
    cluster = ['^','^','^','s','s']
    
    fig, ax = plt.subplots()
    
    for xp, yp, m in zip(x, y, cluster):
        ax.scatter([xp],[yp], marker=m)
    
    plt.show()
    

    A neater solution would be to filter your input data using your cluster information. We can do that using numpy.

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.array([1,2,3,4,5])
    y = np.array([2,1,3,6,7])
    
    cluster = np.array([1,1,1,2,2]) 
    
    fig, ax = plt.subplots()
    
    ax.scatter(x[cluster==1],y[cluster==1], marker='^')
    ax.scatter(x[cluster==2],y[cluster==2], marker='s')
    
    plt.show()
    
    0 讨论(0)
提交回复
热议问题