2012-08-14 94 views
1

Axes3D的bar3d函数有一个“颜色”参数,它可以接受数组来为单独的颜色条着色不同的颜色 - 但是如何应用颜色贴图(即cmap = cm。喷射)与plot_surface函数相同的方式,例如?这将使一个高度的酒吧变成一个反映其身高的颜色。将颜色贴图应用于mpl_toolkits.mplot3d.Axes3D.bar3d

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

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

回答

2

这里是我的解决方案:

offset = dz + np.abs(dz.min()) 
fracs = offset.astype(float)/offset.max() 
norm = colors.normalize(fracs.min(), fracs.max()) 
colors = cm.jet(norm(fracs)) 

ax.bar3d(xpos,ypos,zpos,1,1,dz, color=colors) 

的第一行,如果您的数据为负时,才需要。

代码由这里改编http://matplotlib.sourceforge.net/examples/pylab_examples/hist_colormapped.html

+0

'colors'在赋值之前被引用。 – diffracteD 2015-05-13 10:01:05

+0

颜色必须导入,'输入matplotlib.colors颜色' – lanery 2016-04-30 08:42:10

+0

你在哪里定义dz? dz读起来像差动,是z方向的间隔距离吗? – rhody 2017-10-20 20:50:53

0

可以传递一个彩色阵列到facecolors参数,它可以设置每一个补丁在表面的颜色。

from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
from matplotlib.ticker import LinearLocator, FormatStrFormatter 
import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
ax = fig.gca(projection='3d') 
X = np.arange(-5, 5, 0.25) 
Y = np.arange(-5, 5, 0.25) 
X, Y = np.meshgrid(X, Y) 
R = np.sqrt(X**2 + Y**2) 
Z = np.sin(R) 
colors = np.random.rand(40, 40, 4) 
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors, 
     linewidth=0, antialiased=False) 
ax.set_zlim(-1.01, 1.01) 

ax.zaxis.set_major_locator(LinearLocator(10)) 
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) 

plt.show() 

enter image description here

+0

我在复制cmap = cm.jet的行为之后,但是似乎没有bar3d的这种选项。编辑我的问题来说明清楚。 – Ferguzz 2012-08-14 13:07:00

+0

这个答案在这里提出的问题(它根本不处理问题)方面没有用,最好也可以删除。 – ImportanceOfBeingErnest 2017-10-20 20:55:13

相关问题