Python Forum
Coditional colormaps plotting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coditional colormaps plotting
#1
Dear all,
I need some help with regards to colormaps, I am new to python and unsure how to go about it.

I have three spatial correlation maps, lets call the A(72x144), B(72X144) and C(72x144) and each one of these are made of of values from -1 to 1.

Now I want to generate one single colomap form these based on the following conditions,

For regions where A > 0.5, B > 0.5 and C > 0.5, make those regions red on the new color map, for regions where A>0.5, B = 0 and C = 0 also make those regions Blue.

In a coding format I want something like this
For in range(x):
for J in range(y):
if A(i,j) >0 & if B(i,J) > 0.5 & if C>0.5:
then colormap = Reds.
This is the general idea I have but dont know how to write this and make it actually work with my code.
I am open to anwer further questions and I would be most grateful for your help.

Thanks
Adam.
Reply
#2
You can create rgb-image taking into account conditions you stated.

from matplotlib import pyplot as plt
import numpy as np
A, B, C = np.random.rand(72,144), np.random.rand(72,144), np.random.rand(72,144) # simulate data 
rgb_image = np.zeros(A.shape + (3,))
rgb_image[..., 0] = ((A > 0.5) & (B > 0.5) & (C > 0.5)).astype(int) # red channel
rgb_image[..., 2] = ((A > 0.5) & np.isclose(B, 0) & np.isclose(C, 0)).astype(int)   # blue channel;
plt.imshow(rgb_image)
plt.show()
Reply
#3
(May-03-2019, 11:59 AM)scidam Wrote: You can create rgb-image taking into account conditions you stated.

from matplotlib import pyplot as plt
import numpy as np
A, B, C = np.random.rand(72,144), np.random.rand(72,144), np.random.rand(72,144) # simulate data 
rgb_image = np.zeros(A.shape + (3,))
rgb_image[..., 0] = ((A > 0.5) & (B > 0.5) & (C > 0.5)).astype(int) # red channel
rgb_image[..., 2] = ((A > 0.5) & np.isclose(B, 0) & np.isclose(C, 0)).astype(int)   # blue channel;
plt.imshow(rgb_image)
plt.show()

Thanks very much for your reply, When I tried this, it produces only red colors with black background which is not what I expect. Wanted add picture here for you to see but cant. Anyway to show only red and blue colors on the map?
Reply
#4
Did you comment line #3 in my code? You already have A,B,C datasets so you don't need to generate them. When either B or C are generated using np.random.rand, it is almost impossible that some values in B and C would be simultaneously equal to zero.
Therefore, for simulated data condition np.isclose(B, 0) & np.isclose(C, 0) wouldn't be satisfied, and you will never got
blue colors in rgb_image. Comment line #3 in my code, and use yours A,B,C-matrices instead.
Reply
#5
rgb_image = np.zeros(A.shape + (4,))
rgb_image[..., 0] = (CFAS505>0.5) & (CFAS5095>0.5) & (CFAS595>0.5) # red channel Shift
rgb_image[..., 1] = np.logical_and(CFAS505<0.25,CFAS505> -0.25) & np.logical_and(CFAS5095<0.25,CFAS5095> -0.25) & (CFAS595<-0.5).astype(int)# blue channel;
rgb_image[..., 2] = (CFAS505<0.5) & (CFAS5095>0.5) & (CFAS595<0.5)
rgb_image[..., 3] = ((CFAS505>0.5) &(CFAS5095<0.5) & (CFAS595<0.5))
plt.imshow(rgb_image)
plt.show()
I am trying to add two more colors like this but doesnt work, Also can I add coastline to the plot and make the background transparent instead of black?

Thanks
Reply
#6
I didn't analyze that, but lets suppose that your conditions don't become true simultaneously; In case of using multiple colors
the approach should be slightly different.
COLORS = {'red': [1, 0, 0],  # may be you need to scale values to 255... e.g. red = [255, 0, 0];
          'blue': [0, 0, 1],
          'green': [0, 1, 0],
          'gray' : [0.5, 0.5, 0.5],
    #     ...  define your colors here, 
}

rgb_image = np.zeros(A.shape + (3, ))
rgb_image[(CFAS505>0.5) & (CFAS5095>0.5) & (CFAS595>0.5), :] = COLORS['red']
rgb_image[((CFAS505>0.5) &(CFAS5095<0.5) & (CFAS595<0.5)), :] = COLORS['gray']
# do the same with all conditions and choose appropriate color
...
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020