3

I have two 2D arrays, each of them representing a property on a map. One of them represents hail probability (0%-100%) and the othe one hail severity (0-No Hail, 1-Low, 2-Medium, 3-High).

I can plot this arrays separately with matplotlib's imshow and a predefined colormap:

import matplotlib.pyplot as plt
import matplotlib.colors as cl

cmap = cl.ListedColormap(['#00FF00', '#FFFF00', '#FF0000'])
bounds = [0, 30, 60, 100]
norm = cl.BoundaryNorm(bounds, cmap.N)

plt.subplot(121)
plt.imshow(hail_prob, cmap=cmap, norm=norm)

cmap = cl.ListedColormap(['#00FF00', '#FFFF00', '#FF0000'])
bounds = [0.5, 1.5, 2.5, 3.5]
norm = cl.BoundaryNorm(bounds, cmap.N)

plt.subplot(122)
plt.imshow(hail_sev, cmap=cmap, norm=norm)

This is quite easy as seen above.

However I want a unique plot that combines both features. I have tested the contour function, but the data is quite irregular and the plots look quite bad.

I have been thinking about combining both characteristics into one colormap, but I'm not quite sure about how to do it. Let's say that I want a colour for each combination of probability and severity.

Any ideas on how to do this?

2 Answers2

2

I would do a scatter plot, where the color is one value, and the size is another. For example, the color could be probability but size would be intensity.

Here is some random data

hail_prob = np.random.rand(48, 64)
hail_sev = np.random.randint(0,4,hail_sev.shape)

And here, from your existing data you can grab x-y points with np.meshgrid and use them in the scatter plot:

x = np.arange(hail_prob.shape[1])
y = np.arange(hail_prob.shape[0])
xy = np.meshgrid(x,y)
scatter(*xy, c=hail_prob, s=hail_sev)

You'll have to tweak the normalization on the sizes, because your units will be something different from a good pixel size.

random size and color

Or for a more interesting shape: some other shape

askewchan
  • 45,161
  • 17
  • 118
  • 134
1

I'm not sure how this will turn out but you could use different colormaps and overlay one plot on top of the other and play with the alpha (transparency) of the top one.

Say,

cmap1='Reds'
cmap2='Blues'
plt.imshow(hail_prob, cmap=cmap1, norm=norm)
plt.imshow(hail_sev, cmap=cmap2, norm=norm, alpha=0.5)
plt.colorbar()
Lee
  • 29,398
  • 28
  • 117
  • 170
  • I have been thinking about something similar to what you said, but I don't know if this is possible. My idea is to use a colormap for one property and define the alpha based on the other property. Do you think this is possible? – Iñigo Hernáez Corres Sep 13 '13 at 11:04
  • Yes, I think so. You have to use a `LinearSegmentedColormap` and use the `_lut` property of 'cmap' to set the alphas. See this http://stackoverflow.com/a/10127675/1461850 – Lee Sep 13 '13 at 11:07
  • I have been checking the `_lut` property and it can be used to change the alpha of the colors that define the colormap. However, what I need is to define the alpha of each pixel in the image separately with the data of a 2D array. Something like a pixel based alpha property instead of a scalar that applies to the whole plot. – Iñigo Hernáez Corres Sep 13 '13 at 11:16
  • I think that defining a range of alphas in an array and using `_lut` will should achieve the same thing. I'm not sure if you can define alphas pixel by pixel. – Lee Sep 13 '13 at 11:25
  • http://stackoverflow.com/questions/15207255/is-there-any-way-to-use-bivariate-colormaps-in-matplotlib/15435734#15435734 <- relevant to this discussion – tacaswell Sep 17 '13 at 02:23