[Matplotlib-users] Making color maps with ranges:

empet emilia.petrisor at gmail.com
Sun May 5 12:18:41 EDT 2019


matplotlib - users mailing list wrote
> Dear all,
> I am try to make my own colormap, after searching online including stack
> overflow  there is still no luck hence hope to get some help here,
> 
> Specifically can I devise my own cmap such that from 1-0.5 is red,
> 0.5-0.25
> is blue, 0.25- -0.25 is white, then -0.25- -0.5 green, and last -0.5- -1
> is
> blue
> 
> My data is 2D spatial data with range -1 to 1
> Thanks in ad
> Cheers,
> 
> *Mustapha Adamu*
> 
> _______________________________________________
> Matplotlib-users mailing list

> Matplotlib-users@

> https://mail.python.org/mailman/listinfo/matplotlib-users

Mustapha,

You should define a discrete colormap as follows:

boundaries = [-1, -0.5, -0.25, 0.25, 0.5, 1]  
cmap= colors.ListedColormap(['blue', 'green','white', 'blue', 'red'])
norm = colors.BoundaryNorm(boundaries, cmap.N, clip=True)

Then the following code:

x = np.linspace(0, np.pi, 400)
x, y = np.meshgrid(x, x)
z = np.sin(x*y)
plt.pcolormesh(x, y, z, cmap=cmap, norm=norm)
plt.colorbar()

generates an image as you wanted:

<http://matplotlib.1069221.n5.nabble.com/file/t4189/rsz_1sinxy.png> 



--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html


More information about the Matplotlib-users mailing list