1

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

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. but, I am less experienced in matplotlib. How can I manually set above mentioned colors. Because, I saw only 'straight' colors like 'b' - blue, 'r' - red... in the link you gave me.
Edited to answer your question.
@Spider You can also use HTML colornames. Here's a good list I often use: w3schools.com/html/html_colorvalues.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.