How to create a 3d-bar chart with variable alpha

3.8k views Asked by At

I'm using matplotlib bar3d with RdBu colormap and wanted to have variable transparency between bars (so smaller bars can be more transparent than taller bars).

Here is the code for making the 3d bar plot. The data is stored in a 4x4 matrix 'rho'. At the moment alpha is kept at 0.95, but it would be excellent to be able to control the value of alpha for each bar.

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)
1

There are 1 answers

0
Kirubaharan J On BEST ANSWER

Tested in python 3.11.2, matplotlib 3.7.1

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

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 = plt.cm.RdBu(nrm(-dz))
alpha = np.linspace(0.2, 0.95, len(xpos), endpoint=True)

fig = plt.figure()
ax = fig.add_subplot(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()

enter image description here