user3558855
user3558855

Reputation: 313

Single-valued array to rgba array using custom color map in Python

I have code that, given a normalized array, gives an rgba array using a predefined colormap - jet in the example below.

import numpy as np
from matplotlib import cm

#arr_orig assumed to be normalized, real array.
arr_color = np.uint8(cm.jet(arr_orig) * 255)

I have also defined a custom rgb colormap, my_colormap, based on code in http://matplotlib.org/examples/pylab_examples/custom_cmap.html, and that I gave a name using

cm.register_cmap(name = 'custom_colormap', cmap = my_colormap)

Unsurprisingly, though, I can't now replace my original code with

arr_color = np.uint8(cm.custom_colormap(arr_orig) * 255)

When I try this, I get the following error:

AttributeError: 'module' object has no attribute 'custom_colormap'

What is some code equivalent to

arr_color = np.uint8(cm.custom_colormap(arr_orig) * 255)

that I can use to make a new array using a custom colormap that I have named?

Thanks for any help.

Upvotes: 1

Views: 1049

Answers (1)

user3558855
user3558855

Reputation: 313

The following worked:

arr_color = cm.ScalarMappable(cmap = custom_colormap).to_rgba(arr_orig, bytes = True)

Upvotes: 2

Related Questions