2012-08-14 95 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

相關問題