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
See Question&Answers more detail:
os