1

I want to statically define a mapping for a value to a color. My values are orange, banana, apple (categoricals) . However they don't always all appear together. The following will map the first appearing value to red, second to yellow and so on.

matplotlib.colors.ListedColormap('red','yellow','green')

I am looking for something like this:

matplotlib.colors.ListedColormap({'red':'orange',
                                       'yellow':'banana',
                                       'green':'apple')

However, the later just transforms the keys of that dictionary to a list, resulting in the first ...

I am doing a heatmap where I want to color the background accordingly

BobBetter
  • 195
  • 3
  • 14
  • A colormap always maps the range between 0 and 1 to colors. So you need to map the numbers -1, 0, 1 to the range 0 to 1 (i.e. resulting in 0, 0.5, 1), then you can apply the `ListedColormap(["grey", "red", "green"])` (mind the order!). – ImportanceOfBeingErnest Feb 13 '20 at 20:04
  • @ImportanceOfBeingErnest this sounds very complicated for this simple use case.. how would i do it if i had categoricals (apple, orange, banana) and wanted to map those? (which is really is really what i have here) maybe i am misusing colormap here? – BobBetter Feb 13 '20 at 20:19
  • I guess I answered [a couple of similar questions already](https://stackoverflow.com/search?q=user%3A4124317+*BoundaryNorm*+*unique*). It is a bit complicated, true. – ImportanceOfBeingErnest Feb 13 '20 at 20:45
  • @BobBetter It is still very unclear how you want to use the colormap. Pandas? Seaborn? Just a scatterplot? You need to map each category always to the same number between 0 and 1. You could use a dictionary, either to the normalized number or directly to a color depending on how you want to use the colormap. – JohanC Feb 13 '20 at 21:41
  • Just to understand this correctly: If I pass 3 colors , will that lead to 3 ranges between 0 - 1 , each being 1/3 wide , where the values will be mapped to ? So a value 0.1 would be mapped to the first range which is between 0 - 0.333 and its color ( the first color , here red? ) – BobBetter Feb 14 '20 at 01:38
  • Ok my question is a poorly formulated duplicate of this: https://stackoverflow.com/questions/52284019/map-a-specific-string-value-to-a-specific-color-in-matplotlib-pyplot-imshow , which is answered well by @ImportanceOfBeingErnest . Thank you all – BobBetter Feb 14 '20 at 15:59

0 Answers0