代码之家  ›  专栏  ›  技术社区  ›  maximusdooku

如何阻止Matplotlib重复颜色[[副本]

  •  1
  • maximusdooku  · 技术社区  · 7 年前

    我有这个密码:

    plist = ['p5', 'p14', 'p23', 'p32', 'p41', 'p50', 'p59', 'p68', 'p77', 'p85', 'p95']
    
    
    for pltcount in range(len(plist)):
        plt.plot(data1[pltcount], np.exp(data2)[pltcount], marker='o', label=str(plist[pltcount]))
    plt.legend()
    plt.show()
    

    这是使用 plt.style.use('fivethirtyeight')

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  6
  •   cs95 abhishek58g    7 年前

    彩虹的颜色怎么样?这里的关键是使用 ax.set_prop_cycle

    NUM_COLORS = len(plist)
    
    cm = plt.get_cmap('gist_rainbow')
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_prop_cycle('color', [cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
    # Or,
    # ax.set_prop_cycle(color=[cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
    for i, p in enumerate(plist):
        ax.plot(data1[i], np.exp(data2)[i], marker='o', label=str(p))
    
    plt.legend()
    plt.show()
    

    Borrowed from here . 其他可能的选择。