[Matplotlib-users] making continuous color images with custom colormaps

 [Matplotlib-users] making continuous color images with custom colormaps From: Michael Hearne - 2008-12-30 17:56:02 ```I am posting yet another question about colormaps, as I am having trouble grasping the fundamentals of the way the color model works in Matplotlib. There are many examples on-line of very nice looking continuous color images, such as the one that would be produced by using this code: /delta = 0.005 extent = (-3,4,-4,3) x = arange(-3.0, 4.001, delta) y = arange(-4.0, 3.001, delta) X, Y = meshgrid(x, y) Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) Z = (Z1 - Z2) * 10 #normalize the example Z data to be between 0 and 10 Z = ((Z - Z.min())/(Z.max() - Z.min()))*10 jet() imshow(Z) show() /However, I can't find any similar examples for custom colormaps. Below is some test code I wrote to try to understand this. In it I have hard-coded a color dictionary (suitable for use with a LinearSegmentedColormap), a color list (suitable for use with a ListedColormap), and an array of Z values (appropriate for a Boundary norm). I have tried various combinations of Listed and LinearSegmented colormaps, and they either show patches of very discrete colors, or no colors, or the resulting image blows up when I call savefig(). My goal here is to display the Z data in a continuous colormap where the values are interpolated according to either the color dictionary or color list I have defined. A final side question: Does a tutorial on the matplotlib color model exist anywhere? This would be a really useful resource for me, and perhaps for others. Code is appended below. Thanks, Mike #!/usr/bin/env python ''' Trying to figure out how to make a smooth continuous image with my custom colormap ''' from pylab import * from matplotlib.colors import ListedColormap,LinearSegmentedColormap,Normalize,BoundaryNorm isListed = True delta = 0.005 extent = (-3,4,-4,3) x = arange(-3.0, 4.001, delta) y = arange(-4.0, 3.001, delta) X, Y = meshgrid(x, y) Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) Z = (Z1 - Z2) * 10 #normalize the example Z data to be between 0 and 10 (to match my colormap data) Z = ((Z - Z.min())/(Z.max() - Z.min()))*10 cdict = {'blue': [1.0, 1.0, 1.0, 1.0, 1.0, 0.57647058823529407, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 'green': [1.0, 1.0, 0.80000000000000004, 0.90196078431372551, 1.0, 1.0, 1.0, 0.78431372549019607, 0.56862745098039214, 0.0, 0.0, 0.0], 'red': [1.0, 1.0, 0.74901960784313726, 0.62745098039215685, 0.50196078431372548, 0.47843137254901963, 1.0, 1.0, 1.0, 1.0, 0.78431372549019607, 0.50196078431372548]} clist = array([[ 1. , 1. , 1. ], [ 1. , 1. , 1. ], [ 0.74901961, 0.8 , 1. ], [ 0.62745098, 0.90196078, 1. ], [ 0.50196078, 1. , 1. ], [ 0.47843137, 1. , 0.57647059], [ 1. , 1. , 0. ], [ 1. , 0.78431373, 0. ], [ 1. , 0.56862745, 0. ], [ 1. , 0. , 0. ], [ 0.78431373, 0. , 0. ], [ 0.50196078, 0. , 0. ]]) boundaries = array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 13.]) ```

 [Matplotlib-users] making continuous color images with custom colormaps From: Michael Hearne - 2008-12-30 17:56:02 ```I am posting yet another question about colormaps, as I am having trouble grasping the fundamentals of the way the color model works in Matplotlib. There are many examples on-line of very nice looking continuous color images, such as the one that would be produced by using this code: /delta = 0.005 extent = (-3,4,-4,3) x = arange(-3.0, 4.001, delta) y = arange(-4.0, 3.001, delta) X, Y = meshgrid(x, y) Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) Z = (Z1 - Z2) * 10 #normalize the example Z data to be between 0 and 10 Z = ((Z - Z.min())/(Z.max() - Z.min()))*10 jet() imshow(Z) show() /However, I can't find any similar examples for custom colormaps. Below is some test code I wrote to try to understand this. In it I have hard-coded a color dictionary (suitable for use with a LinearSegmentedColormap), a color list (suitable for use with a ListedColormap), and an array of Z values (appropriate for a Boundary norm). I have tried various combinations of Listed and LinearSegmented colormaps, and they either show patches of very discrete colors, or no colors, or the resulting image blows up when I call savefig(). My goal here is to display the Z data in a continuous colormap where the values are interpolated according to either the color dictionary or color list I have defined. A final side question: Does a tutorial on the matplotlib color model exist anywhere? This would be a really useful resource for me, and perhaps for others. Code is appended below. Thanks, Mike #!/usr/bin/env python ''' Trying to figure out how to make a smooth continuous image with my custom colormap ''' from pylab import * from matplotlib.colors import ListedColormap,LinearSegmentedColormap,Normalize,BoundaryNorm isListed = True delta = 0.005 extent = (-3,4,-4,3) x = arange(-3.0, 4.001, delta) y = arange(-4.0, 3.001, delta) X, Y = meshgrid(x, y) Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) Z = (Z1 - Z2) * 10 #normalize the example Z data to be between 0 and 10 (to match my colormap data) Z = ((Z - Z.min())/(Z.max() - Z.min()))*10 cdict = {'blue': [1.0, 1.0, 1.0, 1.0, 1.0, 0.57647058823529407, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 'green': [1.0, 1.0, 0.80000000000000004, 0.90196078431372551, 1.0, 1.0, 1.0, 0.78431372549019607, 0.56862745098039214, 0.0, 0.0, 0.0], 'red': [1.0, 1.0, 0.74901960784313726, 0.62745098039215685, 0.50196078431372548, 0.47843137254901963, 1.0, 1.0, 1.0, 1.0, 0.78431372549019607, 0.50196078431372548]} clist = array([[ 1. , 1. , 1. ], [ 1. , 1. , 1. ], [ 0.74901961, 0.8 , 1. ], [ 0.62745098, 0.90196078, 1. ], [ 0.50196078, 1. , 1. ], [ 0.47843137, 1. , 0.57647059], [ 1. , 1. , 0. ], [ 1. , 0.78431373, 0. ], [ 1. , 0.56862745, 0. ], [ 1. , 0. , 0. ], [ 0.78431373, 0. , 0. ], [ 0.50196078, 0. , 0. ]]) boundaries = array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 13.]) ```
 Re: [Matplotlib-users] making continuous color images with custom colormaps From: Eric Firing - 2008-12-30 19:10:26 ```Michael Hearne wrote: > I am posting yet another question about colormaps, as I am having > trouble grasping the fundamentals of the way the color model works in > Matplotlib. Mike, I recently added examples/pylab_examples/custom_cmap.py with an extensive docstring, partly stolen from the cookbook, to try to explain and illustrate the use of LinearSegmentedColormap. examples/api/colorbar_only.py gives an example of a ListedColormap, although it sounds to me like what you want really is the LinearSegmentedColormap. Eric ```
 Re: [Matplotlib-users] making continuous color images with custom colormaps From: Michael Hearne - 2008-12-30 19:38:19 ```Looking at Eric's documentation, I now understand that my cdict will not work. I retract my question for now until I can figure out how to make a cdict that looks like what I want. Thanks. Eric Firing wrote: > Michael Hearne wrote: >> I am posting yet another question about colormaps, as I am having >> trouble grasping the fundamentals of the way the color model works in >> Matplotlib. > > Mike, > > I recently added examples/pylab_examples/custom_cmap.py with an > extensive docstring, partly stolen from the cookbook, to try to > explain and illustrate the use of LinearSegmentedColormap. > > examples/api/colorbar_only.py gives an example of a ListedColormap, > although it sounds to me like what you want really is the > LinearSegmentedColormap. > > Eric ```
 Re: [Matplotlib-users] making continuous color images with custom colormaps From: John Hunter - 2008-12-30 19:50:34 ```On Tue, Dec 30, 2008 at 1:10 PM, Eric Firing wrote: > Michael Hearne wrote: >> I am posting yet another question about colormaps, as I am having >> trouble grasping the fundamentals of the way the color model works in >> Matplotlib. > > Mike, > > I recently added examples/pylab_examples/custom_cmap.py with an > extensive docstring, partly stolen from the cookbook, to try to explain > and illustrate the use of LinearSegmentedColormap. Online here http://matplotlib.sourceforge.net/examples/pylab_examples/custom_cmap.html JDH ```