I have a numpy ndarray that looks something like:
[[0, 0.25, 1, ...., 0.5, 0.23 ],
[0.3, 0.75, 1, ..., 0.5, 0.37 ],
...,
...,
[0, 0.25, 1, ...., 0.5, 0.23 ],
[0.3, 0.75, 1, ..., 0.5, 0.37 ]]
Basically every value is in the range 0 – 1.0
I would like to visualize this as a bitmap and currently I have a very slow loop which basically does this:
for i, row in enumerate(data):
for j, val in enumerate(row):
yield val_to_rgb(val)
It then will take the 3-tuple of rgb components and do a PIL putdata on it and create a PNG.
I need to do this many times and this ghetto method is slow, and the colorization is very ugly.
My question is this:
Is there a series of matrix operations I can apply which will yield a colorized matrix containing the raw RGB values?
Which really consists of two questions:
- What is the most efficient transformation I can apply to get RGB tuples from the above matrix
- Is there a “nice” way to convert (0, 1.0) values into a colorized representation?
Edit: Clarification- I’m looking to SAVE this as PNG, not just view it in real time. The reason being that a lot of this is getting executed on a headless machine which I then inspect after the fact.
The output of the current algo looks pretty nasty:

matplotlib has imshow function you can use out-of-the-box.
What you’re doing is usually done via “vectorization”. You define a function and let numpy do the iteration: