Spider
Spider

Reputation: 1007

Setting colors range in colorbar Matplotlib

I have sets of images which represent rainfall intensity. I need to simply plot them with colorbar() in a discretized color. I found some other answers, but I could not find them exactly similar. My problem is that I have to set color ranges according to the given rainfall intensity ranges.

How to set colors based on given values in colorbar? One example for one image was shown below:

No  | Color         |  Rain rate
------------------------------ 
0   |  Not visible  | Under 0.2
1   |  Off-white    | 0.3 - 0.5
2   |  Sky-blue     | 0.6 - 1.5
3   |  Light Blue   | 1.6 - 2.5
4   |  Blue         | 2.6 - 4
5   |  Light Cyan   | 5 - 6
6   |  Cyan         | 7 - 10
7   |  Dark Cyan    | 11 - 15
8   |  Yellow       | 16 - 35 
9   |  Yellow-orange| 36 - 50
10  |  Orange       | 51 - 80
11  |  Orange-red   | 81 - 100
12  |  Red          | 100 - 120
13  |  Dark Red     | 120 - 200
14  |  Maroon       | 200 - 350
15  |  Dark Brown   | over 350

Upvotes: 1

Views: 2008

Answers (1)

Joan Smith
Joan Smith

Reputation: 931

The tool you're looking for is called the ListedColorMap, see its docs here: http://matplotlib.org/api/colors_api.html#matplotlib.colors.ListedColormap

cmap = colors.ListedColormap(['white', 'red'])
bounds=[0,5,10]
norm = colors.BoundaryNorm(bounds, cmap.N)

plt.hist2d(xvals, yvals, cmap=cmap)

In matplotlib, anytime you need custom colors you can replace the color string with an rob string. So, if you want, rather than "red", "#eeefff" just say:

cmap = colors.ListedColormap(['white', "#eeefff"])
bounds=[0,5,10]
norm = colors.BoundaryNorm(bounds, cmap.N)

plt.hist2d(xvals, yvals, cmap=cmap)

Upvotes: 2

Related Questions