Saving an imshow-like image while preserving resolution Saving an imshow-like image while preserving resolution numpy numpy

Saving an imshow-like image while preserving resolution


As you already guessed there is no need to create a figure. You basically need three steps. Normalize your data, apply the colormap, save the image. matplotlib provides all the necessary functionality:

import numpy as npimport matplotlib.pyplot as plt# some data (512x512)import scipy.miscdata = scipy.misc.lena()# a colormap and a normalization instancecmap = plt.cm.jetnorm = plt.Normalize(vmin=data.min(), vmax=data.max())# map the normalized data to colors# image is now RGBA (512x512x4) image = cmap(norm(data))# save the imageplt.imsave('test.png', image)

While the code above explains the single steps, you can also let imsave do all three steps (similar to imshow):

plt.imsave('test.png', data, cmap=cmap)

Result (test.png):

enter image description here