【问题标题】:Matplotlib change colormap tab20 to have three colorsMatplotlib 将颜色图 tab20 更改为具有三种颜色
【发布时间】:2017-05-12 13:00:31
【问题描述】:

Matplotlib 有一些新的非常方便的颜色图 (tab colormap)。我想念的是生成像 tab20b 或 tab20c 这样的颜色图但具有三个色调级别而不是四个的功能?

这个解决方案有点复杂,有没有更简单的?

skip = []
for i in range(0,len(cm.colors)//4+1):
    skip.append(4*i)
# the colormap is called Vega in my Matplotlib version
cm = plt.cm.get_cmap('Vega20c')
cm_skip = [cm.colors[i] for i in range(len(cm.colors)) if i not in skip]

for i, c in enumerate(cm_skip):
    x = np.linspace(0,1)
    y = (i+1)*x + i
    plt.plot(x, y, color=c, linewidth=4)

Matplotlib 中的颜色图:

编辑:SO post 中提供了更通用的方法。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    如果“更容易”是指更短,则可以直接在 numpy 数组上评估颜色图。

    import matplotlib.pyplot as plt
    import numpy as np
    
    colors =  plt.cm.Vega20c( (4./3*np.arange(20*3/4)).astype(int) )
    plt.scatter(np.arange(15),np.ones(15), c=colors, s=180)
    
    plt.show()
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 2017-06-27
    • 1970-01-01
    • 2013-12-01
    • 2017-03-17
    • 2021-06-26
    • 2016-03-05
    相关资源
    最近更新 更多