python-Matplotlib bar3d变量alpha
作者:互联网
我将Matplotlib bar3d与RdBu色彩图一起使用,并希望在各个条之间具有可变的透明度(因此,较小的条可以比较高的条更透明).
这是制作3d条形图的代码.数据存储在4×4矩阵“ rho”中.目前,alpha保持在0.95,但是能够控制每个小节的alpha值将是极好的.
干杯
xpos = np.arange(0,4,1)
ypos = np.arange(0,4,1)
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(4*4)
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = rho.flatten()
nrm=mpl.colors.Normalize(-1,1)
colors=cm.RdBu(nrm(-dz))
alpha = 0.95
ax.bar3d(xpos,ypos,zpos, dx, dy, dz, alpha=alpha, color=colors, linewidth=0)
解决方法:
xpos = np.arange(0,4,1)
ypos = np.arange(0,4,1)
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(4*4)
rho = np.random.random((4,4))
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = rho.flatten()
nrm=mpl.colors.Normalize(-1,1)
colors=cm.RdBu(nrm(-dz))
alpha = np.linspace(0.2, 0.95, len(xpos), endpoint=True)
fig = plt.figure()
ax = fig.gca(projection='3d')
for i in range(len(xpos)):
ax.bar3d(xpos[i],ypos[i],zpos[i], dx[i], dy[i], dz[i], alpha=alpha[i], color=colors[i], linewidth=0)
plt.show()
标签:python,matplotlib,colormap,mplot3d 来源: https://codeday.me/bug/20191009/1877868.html