1

I want to obtain grid with values mapped onto colors. It should be like a probability distribution, where 1.0 corresponds to black and 0.0 to white, grey in between. Reading the answers here I try it out for my case:

from matplotlib import mpl,pyplot
import numpy as np

zvals = np.zeros((10,5))
zvals[5,1] = 0.5
zvals[5,2] = 0.3
zvals[5,3] = 0.7
zvals[5,4] = 5.8

cmap = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',['white','black'],256)

img = pyplot.imshow(zvals,interpolation='nearest', cmap = cmap)

pyplot.colorbar(img,cmap=cmap)
pyplot.show()

I see the problem, because linear mapping treats maximal value from the 2D array as black. But array not necessarily contains value 1.0.

How to set hard boundaries of 0.0 and 1.0, with smooth transition in between?

Community
  • 1
  • 1
beginh
  • 1,133
  • 3
  • 26
  • 36
  • You migh want to use scikit-learns [MinMaxScaler](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html) to preprocess your data (=scaling to [0,1]). – sascha Jul 25 '16 at 01:47
  • thanks, but it does not do the thing. I want to check the data for their [0,1] values, not map them into that interval. – beginh Jul 25 '16 at 02:08

1 Answers1

3

Use the vmin and vmax arguments of imshow:

img = pyplot.imshow(zvals, interpolation='nearest', cmap=cmap, vmin=0, vmax=1)
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214