【问题标题】:Matplotlib color according to class labelsMatplotlib 颜色根据类标签
【发布时间】:2012-09-11 07:26:54
【问题描述】:

我有两个向量,一个带有值,一个带有类标签,例如 1、2、3 等。

我想用红色绘制属于第 1 类的所有点,用蓝色绘制属于第 2 类的点,用绿色绘制属于第 3 类的点等。我该怎么做?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    接受的答案是正确的,但如果您可能想要指定应将哪个类标签分配给特定颜色或标签,您可以执行以下操作。我用颜色条做了一些标签体操,但使情节本身减少到一个很好的单行。这对于绘制使用 sklearn 完成的分类结果非常有用。每个标签都匹配一个 (x,y) 坐标。

    import matplotlib
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = [4,8,12,16,1,4,9,16]
    y = [1,4,9,16,4,8,12,3]
    label = [0,1,2,3,0,1,2,3]
    colors = ['red','green','blue','purple']
    
    fig = plt.figure(figsize=(8,8))
    plt.scatter(x, y, c=label, cmap=matplotlib.colors.ListedColormap(colors))
    
    cb = plt.colorbar()
    loc = np.arange(0,max(label),max(label)/float(len(colors)))
    cb.set_ticks(loc)
    cb.set_ticklabels(colors)
    

    使用this答案的略微修改版本,可以将上述N种颜色概括如下:

    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    
    N = 23 # Number of labels
    
    # setup the plot
    fig, ax = plt.subplots(1,1, figsize=(6,6))
    # define the data
    x = np.random.rand(1000)
    y = np.random.rand(1000)
    tag = np.random.randint(0,N,1000) # Tag each point with a corresponding label    
    
    # define the colormap
    cmap = plt.cm.jet
    # extract all colors from the .jet map
    cmaplist = [cmap(i) for i in range(cmap.N)]
    # create the new map
    cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)
    
    # define the bins and normalize
    bounds = np.linspace(0,N,N+1)
    norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
    
    # make the scatter
    scat = ax.scatter(x,y,c=tag,s=np.random.randint(100,500,N),cmap=cmap,     norm=norm)
    # create the colorbar
    cb = plt.colorbar(scat, spacing='proportional',ticks=bounds)
    cb.set_label('Custom cbar')
    ax.set_title('Discrete color mappings')
    plt.show()
    

    这给出了:

    【讨论】:

    • 你能用 23 种颜色(正如 OP 所说的那样,我也有),比如说 1k 个随机点吗?你的回答很好,我对这张照片表示赞成,但我想知道这是否适用于 >10 个课程。
    • 优秀的答案!比公认的答案更彻底。
    • 如何推广到非 NxN 数据集?即不同的行数和列数
    【解决方案2】:

    假设您将数据保存在二维数组中,这应该可以:

    import numpy
    import pylab
    xy = numpy.zeros((2, 1000))
    xy[0] = range(1000)
    xy[1] = range(1000)
    colors = [int(i % 23) for i in xy[0]]
    pylab.scatter(xy[0], xy[1], c=colors)
    pylab.show()
    

    您还可以设置cmap 属性来控制通过使用颜色图来显示哪些颜色;即将pylab.scatter 行替换为:

    pylab.scatter(xy[0], xy[1], c=colors, cmap=pylab.cm.cool)
    

    可以找到颜色图列表 here

    【讨论】:

    • 其实我的数据有23个这样的标签。因此,我将颜色向量分配为从 0 到 22 的列表形式,向量长度与 xy 相同。但是,我收到一条错误消息,提示序列长度必须为 3 或 4。
    • 您能否在您的问题中添加一些示例代码和错误消息?我已经修改了我放在这里的简单示例,使其具有 1000 个点和 23 个标签。
    • 我在使用绘图功能时没有收到错误消息。不幸的是,我用绘图功能覆盖了我的代码,因此无法复制错误。
    • 嗯。如果没有关于错误是什么的更多信息,我不确定如何更改答案以使其对您更有用。
    • 没问题。目前,我找到了一种解决方法……但不一定是最好的方法。
    【解决方案3】:

    一个简单的解决方案是为每个类分配颜色。通过这种方式,我们可以控制每个类的每种颜色。例如:

    arr1 = [1, 2, 3, 4, 5]
    arr2 = [2, 3, 3, 4, 4]
    labl = [0, 1, 1, 0, 0]
    color= ['red' if l == 0 else 'green' for l in labl]
    plt.scatter(arr1, arr2, color=color)
    

    【讨论】:

      猜你喜欢
      • 2018-07-08
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多