Making color maps with ranges:

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Making color maps with ranges:

matplotlib - users mailing list
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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Making color maps with ranges:

empet
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
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users