如何使用 Matplotlib 获得 N 个易于区分的颜色

And*_* BB 5 python colors matplotlib colormap

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

colors=brg(np.linspace(0,1,num_plots))
Run Code Online (Sandbox Code Playgroud)

for i in range(num_plots):
    ax.step(x,y,c=colors[i])
Run Code Online (Sandbox Code Playgroud)

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

请注意,区分顶部和底部图的颜色是多么困难,如果使用图例,这尤其糟糕。我尝试了很多不同的颜色图,例如彩虹和黑色,但通过此设置,brg 似乎可以提供num_plots1 到 12 之间的最佳结果。

我确实找到了如何获得 10 种易于识别的不同颜色和此 Wiki 页面帮助:可区分的颜色,但我不知道这是否可以以任何方式使用..

有没有简单的解决办法,或者我必须解决这个问题吗?

Diz*_*ahi 6

我会使用tab10tab20颜色图。请参阅色彩图参考

在此输入图像描述

然而,我相信当行数变大时(我会说> 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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述