3

I am looking for way to create a colormap in python/matplotlib that is specific to a designated integer value and remains that integer value. Here is an example of how I define the matlab colormap:

c_map = [1   1   1;...     % Integer assignment = 0 - Air - white
         0.6 1 0.8;... % Integer assignment = 1 - Water - cyan
         1 0.6 0.2];   % Integer assignment = 2 - Sediments - orange

it is later called during the plotting routine by:

colormap(c_map);
pcolor(xvec,zvec,ColorIntegers);
shading interp, colormap, caxis([0 3]), axis ij

The integer values always stay the same (i.e., Air = 0 , Water = 1, sediments = 2).

I've scoured the matplotlib documentation and stack, but haven't found a way to create this specific style colormap, which relates corresponding integers to a color. Most questions deal with diverging, jet, centering, linear, non-linear as opposed to a consistent coloring of specific colormaps. Each color must correspond to that specific integer value.

Any help will be appreciated, thank you in advance.

4
  • 1
    Would it be possible to set it up as a 0, 1, 2, ... map while plotting? mapping.index(material) or something like that. Not a nice hack, but still. Commented Sep 27, 2015 at 8:50
  • Hi Jean, thank you for your comment and really appreciate it. The answer below worked perfect. As I'm not a python guru - that seemed to be the most usable. Thanks again for you comment. Commented Sep 27, 2015 at 10:38
  • It's okay. I'd thought that y was array([["water", "water", "sediment", ...]]), and you wanted to convert it to integers. mapping could be a list (["air", "water", "sediment", "rock"]), or a dict ({"air": 0, "water": 1, "sediment": 2, "rock": 3}). Using mapping.index(material) for a list, or mapping[material] for a dict, would allow you to automatically convert it to numbers for drawing. Commented Sep 27, 2015 at 12:16
  • No worries! That is great to know about using lists. I have a lot to learn apparently. I appreciate your advice. Commented Sep 27, 2015 at 12:35

1 Answer 1

5

Take a look at the ListedColormap.

For example,

In [103]: y
Out[103]: 
array([[1, 1, 2, 2, 2, 1, 0, 0, 0, 1, 2, 2, 2, 2, 2, 1],
       [1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
       [1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0],
       [1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 0],
       [1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 1, 0],
       [1, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 3, 3, 2, 1, 0],
       [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1],
       [1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1],
       [1, 1, 0, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1],
       [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 2],
       [0, 1, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 1, 1, 2, 2],
       [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 2, 2, 2, 2, 2],
       [2, 1, 1, 0, 0, 1, 2, 2, 3, 3, 3, 2, 2, 2, 2, 1],
       [2, 1, 1, 0, 1, 2, 2, 3, 3, 3, 3, 2, 1, 1, 1, 1],
       [2, 1, 1, 0, 2, 3, 3, 3, 3, 3, 2, 1, 0, 1, 1, 0]])

In [104]: c_map = [[1, 1, 1], [0.6, 1, 0.8], [1, 0.6, 0.2], [0.75, 0.25, 0.25]]

In [105]: cm = matplotlib.colors.ListedColormap(c_map)

In [106]: imshow(y, interpolation='nearest', cmap=cm)

generates

image

Sign up to request clarification or add additional context in comments.

Comments

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.