mpl.colors.BoundaryNorm question

Hi,
I'd like to generate a colormap index based on an array of levels & using an
existing colormap (Spectral).
However, Id like the cmap index to start at the 0.3 value of the Spectral
scale (orange/yellow area) instead of starting at the '0' scale value (red
area), and then continue until the 0.8 value area (green)...in essence, Id
like to do a 'slice' of a given colormap, using BoundaryNorm or some other
function, and using my levels array in order to break up the colormap.

What would be the best way to get this done?
Can it be easily done using existing functions, or would I need to create my
own colormap?

Please help,

Thanks,
P.Romero

P.R. wrote:

Hi,
I'd like to generate a colormap index based on an array of levels & using an
existing colormap (Spectral).
However, Id like the cmap index to start at the 0.3 value of the Spectral
scale (orange/yellow area) instead of starting at the '0' scale value (red
area), and then continue until the 0.8 value area (green)...in essence, Id
like to do a 'slice' of a given colormap, using BoundaryNorm or some other
function, and using my levels array in order to break up the colormap.

What would be the best way to get this done?
Can it be easily done using existing functions, or would I need to create my
own colormap?

One way is to extract the colors you want from spectral, and use them with a boundary norm. Suppose you want 10 colors because you have 11 boundaries, and suppose they are in the range from 20 to 30:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import matplotlib.cm as cm

boundaries = np.linspace(20, 30, 11)
colors = cm.spectral(np.linspace(0.3, 0.8, 10))
cmap = mcolors.ListedColormap(colors)
norm = mcolors.BoundaryNorm(boundaries, cmap.N)

z = 20 + np.random.rand(10,20)*10
plt.imshow(z, cmap=cmap, norm=norm)
plt.colorbar()
plt.show()

So yes, you are creating your own colormap, but it is easy.

Eric

···

Please help,

Thanks,
P.Romero

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Eric Firing wrote:

It occurred to me after posting that imshow by default gives a misleading picture of the effect of the cmap and boundary norm. To get a clear picture, add the "interpolation='nearest'" kwarg to the imshow call.

Eric

···

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import matplotlib.cm as cm

boundaries = np.linspace(20, 30, 11)
colors = cm.spectral(np.linspace(0.3, 0.8, 10))
cmap = mcolors.ListedColormap(colors)
norm = mcolors.BoundaryNorm(boundaries, cmap.N)

z = 20 + np.random.rand(10,20)*10
plt.imshow(z, cmap=cmap, norm=norm)
plt.colorbar()
plt.show()

Eric,
Thanks, that worked...

I have a separate question about colormaps...
I'd also like to try creating my own custom colormap by modifying an
existing cmap, inserting/removing colors by changing the values in the rgb
dictionary...

How do I retrieve the actual rgb dictionary associated with, say, cm.jet?

Thanks,
P.Romero

···

-----Original Message-----
From: Eric Firing [mailto:efiring@…202…]
Sent: 2009-08-14 12:49 PM
To: P.R.
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] mpl.colors.BoundaryNorm question

Eric Firing wrote:

It occurred to me after posting that imshow by default gives a
misleading picture of the effect of the cmap and boundary norm. To get
a clear picture, add the "interpolation='nearest'" kwarg to the imshow call.

Eric

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import matplotlib.cm as cm

boundaries = np.linspace(20, 30, 11)
colors = cm.spectral(np.linspace(0.3, 0.8, 10))
cmap = mcolors.ListedColormap(colors)
norm = mcolors.BoundaryNorm(boundaries, cmap.N)

z = 20 + np.random.rand(10,20)*10
plt.imshow(z, cmap=cmap, norm=norm)
plt.colorbar()
plt.show()