python - Pylab colormap generator returns constant lookup table -
i have following data['coeff']
column inside data
array:
[2.57270343, 3.65648059, 2.23084319, 4.80688107, 6.701842, 9.74599594, 7.05068671, 4.01677958, 3.37391547, 3.1511914, 2.38645804, 2.8914971, 3.49830092, 1.44652964, 6.689542, 7.57978187, 10.44210292, 9.96295341, 4.64736146, 3.1432992, 3.57139011, 3.46039262, 3.1436559, 2.56808725, 6.4063686, 7.31260831, 7.26623531, 10.46764653, 6.73590746, 2.7361601, 4.84963666, 5.61582771, 2.86951066, 3.46070112, 5.6345987, 15.91982055, 16.49156807, 11.90533765, 5.11663041, 2.0236397, 9.56519465, 7.69136698, 6.05681034, 4.23944285, 11.4375905, 23.21954318, 25.07053804, 14.68918469, 3.85383511, 2.2003411, 5.09153196, 6.61014214, 10.88575876, 5.09483765, 18.3454972, 27.26258116, 25.72212513, 8.84484278, 1.57545174, 1.8912232, 16.78862314, 20.29314535, 12.44983752, 3.6037403, 3.0240955, 2.30155483, 1.89219047, 5.04316851, 5.45651532, 5.4099686, 6.20357723, 3.29846311, 1.83343417, 5.46524273, 5.9786796, 4.16043566, 5.28458388, 3.69230367, 1.12351996, 3.5924855, 2.18969866, 6.26344489, 6.56806219, 4.03959973]
when pass these data pylab.cm.rdbu(data['coeff'])
function gives me constant table (each point has same color):
array([[ 0.01960784, 0.18823529, 0.38039216, 1. ], [ 0.01960784, 0.18823529, 0.38039216, 1. ], [ 0.01960784, 0.18823529, 0.38039216, 1. ], ... [ 0.01960784, 0.18823529, 0.38039216, 1. ]])
it seems went wrong because when use scatter
cmap
works correctly (i.e. points colored , scaled properly):
plt.scatter(data['x'], data['y'], c=data['coeff'], cmap='rdbu', marker='o')
matplotlib colormaps normalized between 0 , 1. value 0 <= x <= 1
give color colormap. if apply colormap value x < 0
, return value corresponding 0
, if apply colormap value x > 1
, return value corresponding 1
.
latter case happening here. since values list above 1, essentiall same result as
plt.cm.rdbu(np.ones(len(data)))
most plotting commands take colormap input use normalization internally map values range [0,1]
first, apply colormap. normalization can given externally in commands, e.g.
plt.scatter(..., c=data, cmap='rdbu', vmin=1.2, vmax=11.5)
or
norm = plt.normalize(vmin=1.2, vmax=11.5) plt.scatter(..., c=data, cmap='rdbu', norm=norm)
applying colormap data directly hence require use normalization.
norm = plt.normalize(vmin=data.min(), vmax=data.max()) plt.cm.rdbu(norm(data))
Comments
Post a Comment