I am able to plot the RGB components of some Matplotlib's colour maps with this simple Python script:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
mapa = cm.get_cmap('viridis', 256)
R = []; G = []; B = []
ind = np.linspace(1,256,256)
for item in mapa.colors:
R.append(item[0])
G.append(item[1])
B.append(item[2])
plt.figure(1,figsize=(8,8))
plt.plot(ind,R,'r-')
plt.plot(ind,G,'g-')
plt.plot(ind,B,'b-')
plt.xlabel('$Colour \\ index$', fontsize=16, fontname="Times")
plt.ylabel('$RGB \\ component \\ weight$', fontsize=16, fontname="Times")
plt.show()
Some, but not all. It works OK with 'viridis', but not with the notorious 'jet', or 'prism', or 'summer' colour maps. This happens because (it seems) those maps do not have the attribute 'colors':
runfile('F:/Documents/Programs/Python/Colourmap_Plot.py', wdir='F:/Documents/Programs/Python') Traceback (most recent call last):
File "F:\Documents\Programs\Python\Colourmap_Plot.py", line 37, in for item in mapa.colors:
AttributeError: 'LinearSegmentedColormap' object has no attribute 'colors'
I wonder why that happens. Shouldn't all maps be equal with regard to their structure? How could I tell maps that do have the 'colors' attribute from the ones that haven't? And finally, how to plot the components from one of those 'non-conforming' maps?