I have a custom ROYGBIV rainbow colormap:
import numpy as np
import matplotlib.pyplot as plt
# dictionary with RGB values
ROYGBIV = {
'blue': ((0.0, 1.0, 1.0),
(0.167, 1.0, 1.0),
(0.333, 1.0, 1.0),
(0.5, 0.0, 0.0),
(0.667, 0.0, 0.0),
(0.833, 0.0, 0.0),
(1.0, 0.0, 0.0)),
'green': ((0.0, 0.0, 0.0),
(0.167, 0.0, 0.0),
(0.333, 0.0, 0.0),
(0.5, 1.0, 1.0),
(0.667, 1.0, 1.0),
(0.833, 0.498, 0.498),
(1.0, 0.0, 0.0)),
'red': ((0.0, 0.5608, 0.5608),
(0.167, 0.4353, 0.4353),
(0.333, 0.0, 0.0),
(0.5, 0.0, 0.0),
(0.667, 1.0, 1.0),
(0.833, 1.0, 1.0),
(1.0, 1.0, 1.0))}
### creates colormap using Matplotlib
mycmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',ROYGBIV,256)
I tested it using this code:
### creates random checker pattern to test colormap
plt.figure(figsize=(5,4))
plt.pcolor(rand(10,10),cmap=mycmap)
plt.colorbar()
plt.show()
L has the values for Lightness for the colormap
L=
[42.853117895072565,
38.896632760964955,
32.299375201436156,
87.73500278716472,
97.13881604341229,
66.66212771669841,
53.23896002513146]
What I want to do is to plot L then color it according to its values and using mycmap as a colormap. But now comes the trouble. In the past I used successfully this great Colorline function http://nbviewer.ipython.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb for plots similar to those in the notebook, but in this case, with my custom colormap, it fails:
# Interpolating L to add points to make 256_
x = np.arange(7)
from scipy.interpolate import interp1d
import scipy as sp
new_x = np.linspace(x.min(), x.max(), 256)
new_L = sp.interpolate.interp1d(x, L)(new_x)
colorline(new_x,new_L,linewidth=6,cmap="mycmap")
plt.xlim(X.min(), X.max())
plt.ylim(0, 100)
plt.show()
I thought it would work, but instead I get this bunch of error messages:
ValueError Traceback (most recent call last)
<ipython-input-48-ff20dd713b90> in <module>()
----> 4 colorline(new_x,new_L,linewidth=6,cmap="my_cmap")
5 colorline(X,L,linewidth=6)
6 plt.xlim(X.min(), X.max())
<ipython-input-3-58e291da3158> in colorline(x, y, z, cmap, norm, linewidth, alpha)
39
40 segments = make_segments(x, y)
---> 41 lc = LineCollection(segments, array=z, cmap=cmap, norm=norm, linewidth=linewidth, alpha=alpha)
42
43 ax = plt.gca()
/Users/.../python2.7/site-packages/matplotlib/collections.pyc in __init__(self, segments,
linewidths, colors, antialiaseds, linestyles, offsets, transOffset, norm, cmap,
pickradius, zorder, **kwargs)
1012 pickradius=pickradius,
1013 zorder=zorder,
-> 1014 **kwargs)
1015
1016 self.set_segments(segments)
/Users/.../python2.7/site-packages/matplotlib/collections.pyc in __init__(self,
edgecolors, facecolors, linewidths, linestyles, antialiaseds, offsets, transOffset, norm,
cmap, pickradius, hatch, urls, offset_position, zorder, **kwargs)
101 """
102 artist.Artist.__init__(self)
--> 103 cm.ScalarMappable.__init__(self, norm, cmap)
104
105 self.set_edgecolor(edgecolors)
/Users/.../python2.7/site-packages/matplotlib/cm.pyc in __init__(self, norm, cmap)
193 self.norm = norm
194 #: The Colormap instance of this ScalarMappable.
--> 195 self.cmap = get_cmap(cmap)
196 #: The last colorbar associated with this ScalarMappable. May be None.
197 self.colorbar = None
/Users/.../python2.7/site-packages/matplotlib/cm.pyc in get_cmap(name, lut)
159 return _generate_cmap(name, lut)
160
--> 161 raise ValueError("Colormap %s is not recognized" % name)
162
163
ValueError: Colormap my_cmap is not recognized
I do not understand these error messages. I am confused by the fact that the statements seem to point not to Colorline but rather to MatplotLib, but I have created the colormap using MatplotLib. I have also tried this method to created the colormap, but in the end I get similar error messages:
my_cmap2 = matplotlib.colors.ListedColormap(rgb, name='roygbiv1')
My question is in two parts: 1) can anyone suggest what is happening and perhaps recommend some changes to my code? 2) if not, is there any other method to color L according to its value and with the custom colormap? Thanks a lot.