Making color maps with ranges:

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*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190505/ec10c683/attachment.html>

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@

Matplotlib-users Info Page

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&gt;

ยทยทยท

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