Given an array of 2D numbers with real numbers, how to create an image that reflects the intensity of each number?

I have a numpy 2D array and you want to create an image so that pixels corresponding to numbers that have a high value (relative to other pixels) are painted with a more intense color. For example, if the image is in gray scale, and the pixel has a value of 0.4849, and all other pixels correspond to values ​​below 0.001, then this pixel is likely to be painted black or something close to black.

Here is an example image, an array of 28x28 and contains values ​​from 0 to 1.

Everything I did to build this image was done using the following code:

import matplotlib.pyplot as plt im = plt.imshow(myArray, cmap='gray') plt.show() 

enter image description here

However, for some reason this only works if the values ​​are between 0 and 1. If they are on a different scale, which may contain negative numbers, then the image does not make much sense.

+6
source share
2 answers

You can also use different color maps, as in the example below (note that I removed the interpolation):

 happy_array = np.random.randn(28, 28) im = plt.imshow(happy_array, cmap='seismic', interpolation='none') cbar = plt.colorbar(im) plt.show() 

enter image description here

And even gray will work:

 happy_array = np.random.randn(28, 28) im = plt.imshow(happy_array, cmap='gray', interpolation='none') cbar = plt.colorbar(im) plt.show() 

enter image description here

+2
source

You can normalize data in the range (0,1) by dividing everything by the maximum value of the array:

  normalized = array / np.amax(a) plt.imshow(normalized) 

If the array contains negative values, you have two logical options. Build the scale:

  mag = np.fabs(array) normalized = mag / np.amax(mag) plt.imshow(normalized) 

or shift the array so that everything is positive:

 positive = array + np.amin(array) normalized = positive / np.amax(positive) plt.imshow(normalized) 
+1
source

All Articles