1

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.

MyCarta
  • 808
  • 2
  • 12
  • 37
  • 5
    You're going to get more answers on this if you spend a little time creating a [minimal example](http://www.sscce.org) that people can use to replicate the errors. That said, I think the problem is that you are specifying your custom colormap as a string. Should work if you do `cmap=mycmap` instead of `cmap='mycmap'` – chthonicdaemon May 02 '14 at 04:37
  • 1
    also see http://stackoverflow.com/questions/13622909/matplotlib-how-to-colorize-a-large-number-of-line-segments-as-independent-gradi/13649811#13649811 – tacaswell May 02 '14 at 12:26
  • and http://stackoverflow.com/questions/19868548/set-line-colors-according-to-colormap/19868688#19868688 – tacaswell May 02 '14 at 12:27
  • @ chthonicdaemon - thanks for your suggestion on trimming. I think it is better now as it cuts more quickly into the problem. As for using specifying it as a string, that's how it is used in all examples in the Colorline notebook. I'l lgive it a shot tonigth. – MyCarta May 02 '14 at 14:13
  • @ tcaswell thanks for pointing to that example. I'll look at it. If I can't get Colorline to work this may be the approach to use. I n any case, good to add to my personal knowledgebase. – MyCarta May 02 '14 at 14:17
  • If you look at the code for `colorline` this is _exactly_ what it is doing underneath. And it just passes the cmap kwarg through to `LineCollection`. The way that the colormap machinery works if you pass a colormap object then it is happy, if you pass a string it uses `get_cmap` to look up the colormap object. – tacaswell May 02 '14 at 14:46

2 Answers2

1

Your problem is that you created, but did not register, your color map (doc)

mycmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',ROYGBIV,256)
from matplotlib.cm import register_cmap
register_cmap(cmap=mycmap)

The way that the colormap machinery works if you pass a colormap object then it is happy, if you pass a string it uses get_cmap to look up the colormap object. If you do not register it, then matplotlib correctly raises an error when you (via colorline) ask it to give you a colormap it does not know about.

If you look at the code for colorline at the link you posted you will see that it is a very thin wrapper around LineCollection.

tacaswell
  • 84,579
  • 22
  • 210
  • 199
1

Matteo, if you replace the first line of your fifth code block with the following, it should work:

z =  (new_x - np.amin(new_x) )/ new_x.ptp()
colorline(new_x,new_L, z, cmap=mycmap, linewidth=10)

Doing that in addition to @tcaswell's response will get you what you need.