6

I use the colormap in python to plot and analyse values in a matrix. I need to associate the white color to each element equal to 0.0 while for others I'd like to have a "traditional" color map. Looking at Python Matplotlib Colormap I modified the dictionary used by pcolor as:

dic = {'red': ((0., 1, 1), 
               (0.00000000001, 0, 0), 
               (0.66, 1, 1), 
               (0.89,1, 1), 
               (1, 0.5, 0.5)), 
       'green': ((0., 1, 1), 
                (0.00000000001, 0, 0), 
                (0.375,1, 1), 
                (0.64,1, 1), 
                (0.91,0,0), 
                (1, 0, 0)), 
       'blue': ((0., 1, 1), 
               (0.00000000001, 1, 1), 
               (0.34, 1, 1), 
               (0.65,0, 0), 
               (1, 0, 0))}

The result is:enter image description here

I set:

matrix[0][0]=0 matrix[0][1]=0.002

But as you can see they are both associated with the white color, even if I set 0.00000000001 as the starting point for the blue. How is this possible? How can I change it in order to obtain what I'd like?

Community
  • 1
  • 1
Titus Pullo
  • 3,751
  • 15
  • 45
  • 65

1 Answers1

3

Although not ideal, masking the zero value works. You can control the display of it with the cmap.set_bad().

from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt
import numpy as np

dic = {'red': ((0., 1, 0), 
               (0.66, 1, 1), 
               (0.89,1, 1), 
               (1, 0.5, 0.5)), 
       'green': ((0., 1, 0), 
                (0.375,1, 1), 
                (0.64,1, 1), 
                (0.91,0,0), 
                (1, 0, 0)), 
       'blue': ((0., 1, 1), 
               (0.34, 1, 1), 
               (0.65,0, 0), 
               (1, 0, 0))}

a = np.random.rand(10,10)
a[0,:2] = 0
a[0,2:4] = 0.0001

fig, ax = plt.subplots(1,1, figsize=(6,6))

cmap = LinearSegmentedColormap('custom_cmap', dic)
cmap.set_bad('white')

ax.imshow(np.ma.masked_values(a, 0), interpolation='none', cmap=cmap)

enter image description here

Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97
  • Can tell me exactly where do you set the option for 'discontinuities'? Is the interpolation = 'none' ? – Titus Pullo Apr 23 '13 at 12:48
  • Its in your colormap dictonairy. The dictonairy has a format of (x,y1,y2), note that for yours y1 and y2 are equal and mine change. In this case, with my colormap, it means that values _till_ zero have a color of 1 for rgb (so white), but values _from_ zero have 0 for red and green and 1 for blue (so blue). See: http://matplotlib.org/api/colors_api.html#matplotlib.colors.LinearSegmentedColormap – Rutger Kassies Apr 23 '13 at 13:05
  • But if I define the dictionary as you did, I also need to use: np.ma.masked_values(matrix,0) as parameter of imshow(), otherwise the "trick" doesn't work. – Titus Pullo Apr 23 '13 at 13:28
  • Sorry, i mixed up two things in my code. I have edited my post, the masked array approach is the only way i can get it to work. Ignore the things about the cmap, thats indeed not working. Would be nice to have a 'cmap only' solution. – Rutger Kassies Apr 23 '13 at 13:42