Issue
I have the following colorbar :
from matplotlib import cm
cmap = matplotlib.cm.Blues
bounds = [0, 500, 1000, 1500, 2000, 2500]
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N , extend = 'max')
plt.colorbar(matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap), orientation='vertical')
Is there a way to get the hex codes of the six colors generated?
Thanks!
Solution
The norm
object has a property boundaries
- normalise those to the original cmap range, then cmap
can convert those to RGBA 4-tuples, and then you can call matplotlib.colors.rgb2hex
. Putting it all into a list comprehension gets you:
[matplotlib.colors.rgb2hex(cmap(norm(c))) for c in norm.boundaries]
Result:
['#f7fbff', '#d0e1f2', '#94c4df', '#4a98c9', '#1764ab', '#08306b']
Answered By - Josh Friedlander
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.