Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apply color map to mpl_toolkits.mplot3d.Axes3D.bar3d

There is a 'color' argument to Axes3D's bar3d function which can accept arrays to color individual bars different colors - but how would I apply a color map (i.e. cmap = cm.jet) in the same way as a plot_surface function for example ? This would make a bar of a certain height a color which reflects its height.

http://matplotlib.sourceforge.net/examples/mplot3d/hist3d_demo.html

http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/api.html

like image 924
Ferguzz Avatar asked Aug 14 '12 10:08

Ferguzz


1 Answers

Following up the answer provided by Ferguzz, here is a more complete/up-to-date solution:

import matplotlib.colors as colors
import matplotlib.cm as cm


dz = height_values
offset = dz + np.abs(dz.min())
fracs = offset.astype(float)/offset.max()
norm = colors.Normalize(fracs.min(), fracs.max())
color_values = cm.jet(norm(fracs.tolist()))
ax.bar3d(xpos,ypos,zpos,1,1,dz, color=color_values)

Please pay attention to the following points:

  • You should have all variables (such as xpos, ypos) defined similar to the code in https://matplotlib.org/examples/pylab_examples/hist_colormapped.html

  • normalize() is now Normalize()

  • fracs is in type Series (from pandas) and must be converted to list

like image 188
AMj Avatar answered Oct 12 '22 11:10

AMj