代码之家  ›  专栏  ›  技术社区  ›  Mr. Who

matplotlib散点图中的颜色问题

  •  1
  • Mr. Who  · 技术社区  · 8 年前

    这个很好用

    import matplotlib.pyplot as plt
    import numpy as np
    
    y= [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
    x= [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]
    area= [0.78, 0.81, 0.78, 0.81, 0.78, 0.81, 0.787, 0.81, 0.99, 0.999, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99]
    area = np.array(area)
    area = area*2000
    
    cluster = ['*','.','<','>','o','p','H','D','1','2','o','*','*','o','o','o']
    
    fig,ax = plt.subplots()
    
    for xp, yp, m in zip(x, y, cluster):
        ax.scatter(xp, yp,  s=area , marker = m)
    
    plt.show()
    

    import matplotlib.pyplot as plt
    import numpy as np
    
    y= [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
    x= [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]
    colors= [286.135, 288.556, 286.135, 288.55, 286.13, 288.55627, 286.13, 288.556, 342.713, 333.98, 342.713, 333.9834, 342.713, 333.9834, 342.71, 333.98]
    colors = np.array(colors)
    area= [0.78, 0.81, 0.78, 0.81, 0.78, 0.81, 0.787, 0.81, 0.99, 0.999, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99]
    area = np.array(area)
    area = area*2000
    
    cluster = ['*','.','<','>','o','p','H','D','1','2','o','*','*','o','o','o']
    
    fig,ax = plt.subplots()
    
    for xp, yp, m in zip(x, y, cluster):
        ax.scatter(xp, yp, c=colors, s=area, cmap=plt.cm.jet , marker = m)
    
    plt.show()
    

    python说“颜色数组必须是二维的”

    plt.scatter(xp, yp, c=colors, s=area, cmap=plt.cm.jet , marker = 'o')
    

    光谱工作正常,有什么问题?

    1 回复  |  直到 8 年前
        1
  •  0
  •   ImportanceOfBeingErnest    8 年前

    您将分别绘制每个点作为其自己的散点图。这可能确实有道理,能够给每个点自己的标记。然而,这也需要给每个点赋予其自身的大小和颜色。因此,您需要在 colors area 阵列。为了使颜色符合指定的颜色映射,还需要进行归一化。

    import matplotlib.pyplot as plt
    import numpy as np
    
    y= [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
    x= [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]
    colors= [286.135, 288.556, 286.135, 288.55, 286.13, 288.55627, 286.13, 288.556, 342.713, 333.98, 342.713, 333.9834, 342.713, 333.9834, 342.71, 333.98]
    colors = np.array(colors)
    area= [0.78, 0.81, 0.78, 0.81, 0.78, 0.81, 0.787, 0.81, 0.99, 0.999, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99]
    area = np.array(area)
    area = area*2000
    
    cluster = ['*','.','<','>','o','p','H','D','1','2','o','*','*','o','o','o']
    
    fig,ax = plt.subplots()
    
    norm=plt.Normalize(colors.min(), colors.max())
    for xp, yp, m,a,c in zip(x, y, cluster, area, colors):
        sc = ax.scatter(xp, yp, c=c, s=a, cmap=plt.cm.jet , marker = m, norm=norm)
    
    plt.colorbar(sc)
    plt.show()
    

    enter image description here