Matplotlib change colormap tab20 to have three colors


Matplotlib has some new and very handy colormaps (tab colormap). What I miss is a feature to generate a colormap like tab20b or tab20c but with three hue levels instead of four ?

This solution is a bit complicated, is there an easier one ?

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

The colormap in Matplotlib: enter image description here

EDIT: A more generic approach has been provided in this SO post.


Answers:


If by "easier" you mean shorter, you can directly evaluate the colormap on a numpy array.

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