如何使用matplotlib根据条形高度绘制具有特定颜色的3D条形图

时间:2018-05-06 19:15:04

标签: python python-3.x matplotlib 3d

enter image description here

嘿伙计们,我正在使用matplotlib来绘制3D条形图。我想知道是否可以根据它们的值(dz)给每个条(总共9个)一个不同的颜色,并且还显示该图中的相应级别。

非常感谢提前!!!

我的代码如下:

fig_stat=plt.figure(dpi=100)
ax_stat = fig_stat.add_subplot(111, projection='3d')

xpos = [1, 1, 1, 3, 3, 3, 5, 5, 5]
ypos = [1, 3, 5, 1, 3, 5, 1, 3, 5]
# initial z position (x-y plane), np.zeros(x), x
zpos = [0, 0, 0, 0, 0, 0, 0, 0, 0]
# x is the number of points you need

dx = np.ones(9)
dy = np.ones(9)
# dz = [1, 2, 3, 4, 10, 6, 7, 8, 9]  # signal intensity
dz = col_stat.iloc[:, -2]
ax_stat.set_xlabel('X axis')
ax_stat.set_ylabel('Y axis')
ax_stat.set_zlabel('Signal Intensity')
ax_stat.bar3d(xpos, ypos, zpos, dx, dy, dz, color='#00ceaa')
plt.title("Position_{}".format(L)) 

1 个答案:

答案 0 :(得分:1)

return ( <table> <tbody> <tr> <th colSpan='6'>{s.subSectionTitle}</th> </tr> <tr> <td>{s.data1}</td> <td>{s.data2}</td> <td>{s.data3}</td> <td>{s.data4}</td> <td>{s.data5}</td> <td>{s.data6}</td> </tr> </tbody> </table> <div className='pagebreak'></div> <table> <tbody> <tr> <th colSpan='6'>{s.subSectionTitle}</th> </tr> <tr> <td>{s.data1}</td> <td>{s.data2}</td> <td>{s.data3}</td> <td>{s.data4}</td> <td>{s.data5}</td> <td>{s.data6}</td> </tr> </tbody> </table> ) 方法允许以与数据数组相同长度的数组作为颜色输入。因此,可以使用色彩映射来获取与每个bar3d对应的颜色:

dz

并使用此from matplotlib import cm from matplotlib.colors import Normalize cmap = cm.get_cmap('plasma') norm = Normalize(vmin=min(dz), vmax=max(dz)) colors = cmap(norm(dz)) 作为bar3d颜色参数中的输入而不是{ {1}}。然后,可以使用以下代码显示颜色条:

colors

这将生成如下图:

bar cmap

相关问题