In some functions of matplotlib
, we have to pass an color
argument instead of a cmap
argument, like bar3d
.
So we
The answer to your question is given in the snipplet of the documentation that you copied into your question:
...from the interval [0, 1] to the RGBA color...
But if you find your code ugly you could try to make it nicer:
You don't have to specify the limits to the normalization manually (iff you intent to use min/max):
norm = plt.Normalize()
colors = plt.cm.jet(norm(dz))
If you find that ugly (I don't understand why, though), you could go on and do it manually):
colors = plt.cm.jet(np.linspace(0,1,len(dz)))
However this is solution is limited to equally spaced colors (which is what you want given the dz
in your example.
And then you can also replicate the functionality of Normalize
(since you seem to not like it):
lower = dz.min()
upper = dz.max()
colors = plt.cm.jet((dz-lower)/(upper-lower))
Use a helper function:
def get_colors(inp, colormap, vmin=None, vmax=None):
norm = plt.Normalize(vmin, vmax)
return colormap(norm(inp))
Now you can use it like this:
colors = get_colors(dz, plt.cm.jet)