Matplotlib更改colormap tab20有三种颜色

时间:2017-05-12 13:00:32

标签: python matplotlib

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)[![enter image description here][2]]

enter image description here

Matplotlib中的色彩映射: enter image description here

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

1 个答案:

答案 0 :(得分:5)

如果“更简单”意味着更短,则可以直接评估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()

enter image description here