In some functions of matplotlib
, we have to pass an color
argument instead of a cmap
argument, like bar3d
.
So we have to generate a Colormap
manually. If I have a dz
array like this:
dz = [1,2,3,4,5]
What I want is:
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=cm.jet(dz), zsort='average')
However, It does not work, it seems Colormap
instances can only convert normalized arrays.
>>> dz = [1,2,3,4,5]
>>> cm.jet(dz)
array([[ 0. , 0. , 0.51782531, 1. ],
[ 0. , 0. , 0.53565062, 1. ],
[ 0. , 0. , 0.55347594, 1. ],
[ 0. , 0. , 0.57130125, 1. ],
[ 0. , 0. , 0.58912656, 1. ]])
Of course, this is not what I want.
I have to do things like this:
>>> cm.jet(plt.Normalize(min(dz),max(dz))(dz))
array([[ 0. , 0. , 0.5 , 1. ],
[ 0. , 0.50392157, 1. , 1. ],
[ 0.49019608, 1. , 0.47754586, 1. ],
[ 1. , 0.58169935, 0. , 1. ],
[ 0.5 , 0. , 0. , 1. ]])
How ugly the code is!
In matplotlib's document it is said:
Typically Colormap instances are used to convert data values (floats) from the interval [0, 1] to the RGBA color that the respective Colormap represents. For scaling of data into the [0, 1] interval see matplotlib.colors.Normalize. It is worth noting that matplotlib.cm.ScalarMappable subclasses make heavy use of this data->normalize->map-to-color processing chain.
So why I can't use just cm.jet(dz)
?
Here are the imports that I am using
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
Scientifically, the human brain perceives various intuition based on the different colors they see. Matplotlib provides some nice colormaps you can use, such as Sequential colormaps, Diverging colormaps, Cyclic colormaps, and Qualitative colormaps.
Colormap. The new default colormap used by matplotlib. cm. ScalarMappable instances is 'viridis' (aka option D).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With