【问题标题】:How to get N easily distinguishable colors with Matplotlib如何使用 Matplotlib 获得 N 种易于区分的颜色
【发布时间】:2020-05-20 08:02:17
【问题描述】:

我需要使用 Matplotlib 制作不同数量的线图,但我一直无法找到可以轻松区分线图的颜色图。 我已经像这样使用了 brg 颜色图:

colors=brg(np.linspace(0,1,num_plots))

for i in range(num_plots):
    ax.step(x,y,c=colors[i])

有四个图,可能如下所示:

请注意区分顶部和底部图的颜色是多么困难,如果使用图例,这尤其糟糕。 我尝试了很多不同的颜色图,比如 Rainbow 和 jet,但是通过这种设置,brg 似乎在 1 到 12 之间为 num_plots 提供了最好的结果。

我确实找到了这个 How to get 10 different colors that are easily recognizable 和这个 Wiki 页面 Help:Distinguishable colors,但我不知道这是否可以以任何方式使用..

是否有一个简单的解决方法,或者我必须解决这个问题?

【问题讨论】:

    标签: python matplotlib colors


    【解决方案1】:

    我会使用tab10tab20 颜色图。见Colormap reference

    但是,我相信当线条数量变大时(我会说 >5 并且肯定 >10),您将始终难以区分色调。 在这种情况下,您应该将色调与其他显着特征(例如不同的标记或线条样式)结合起来。

    colors = matplotlib.cm.tab20(range(20))
    markers = matplotlib.lines.Line2D.markers.keys()
    x = np.linspace(0,1,100)
    
    fig, axs = plt.subplots(2,4, figsize=(4*4,4*2))
    for nlines,ax0 in zip(np.arange(5,21,5), axs.T):
        ax0[0].set_title('{:d} lines'.format(nlines))
        for n,c,m in zip(range(nlines),colors,markers):
            y = x*np.random.random()+np.random.random()
            ax0[0].plot(x,y)
            ax0[1].plot(x,y, marker=m, markevery=10)
    axs[0,0].set_ylabel('only hues', fontsize=16, fontweight='bold')
    axs[1,0].set_ylabel('hues+markers', fontsize=16, fontweight='bold')
    fig.tight_layout()
    

    【讨论】:

    • 啊,看起来不错 - 我不知道 markevery 选项 - 现在这只是标记大小的问题,因为 s=50 与标记有很大不同。不过还是谢谢:)
    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2021-10-22
    • 2021-12-07
    相关资源
    最近更新 更多