I am trying to plot some colorful discrete data using matplotlib
.
With tab10-like
colormaps I get nice results
However, I would need a combination of tab20 and tab20b parts, to have my data plotted as:
1->tab20darkblue
2->tab20lightblue
3->tab20cOrange1
4->tab20cOrange2
5->tab20cOrange3
6->tab20cOrange4
is that possible somehow?
You can create a ListedColormap
from the colors of other colormaps.
E.g to get the darkorange color from the tab20c
colormap, use plt.cm.tab20c(4)
(as this is the 5th color in that map). Note that this works only for indexed colormaps - otherwise you need to use a value between 0 and 1.
From a list of thus obtained colors, you can create a new ListedColormap
.
import matplotlib.pyplot as plt
import matplotlib.colors
import numpy as np
colors = [plt.cm.tab20(0),plt.cm.tab20(1),plt.cm.tab20c(4),
plt.cm.tab20c(5),plt.cm.tab20c(6),plt.cm.tab20c(7)]
cmap=matplotlib.colors.ListedColormap(colors)
x = np.arange(1,7)
plt.scatter(x,x,c=x, s=100, cmap=cmap, vmin=1, vmax=7)
plt.show()
Or, to get a nice colorbar as well,
import matplotlib.pyplot as plt
import matplotlib.colors
import numpy as np
colors = [plt.cm.tab20(0),plt.cm.tab20(1),plt.cm.tab20c(4),
plt.cm.tab20c(5),plt.cm.tab20c(6),plt.cm.tab20c(7)]
cmap=matplotlib.colors.ListedColormap(colors)
norm = matplotlib.colors.BoundaryNorm(np.arange(1,8)-0.5,len(colors))
x = np.arange(1,7)
sc = plt.scatter(x,x,c=x, s=100, cmap=cmap, norm=norm)
plt.colorbar(sc, ticks=x)
plt.show()