title: weird 3d bar plot for given data using matplotlib
body: <p>I am trying to 3d plot below data, with height being the respecitive joint probability from probability mass function. The idea is to visualize covariance. I had to go 3D because, the probabilities varies for different combinations of sample. The bars or boxes overlap each other in weird ways that I am unable to infer a proper 3d perspective in different angles. If you look at below gif you will know (box suddenly grows over each other at few angles out of nowhere). Kindly help how to resolve this issue. Also alpha is not working.  </p>
  
  <p><strong>Issues:</strong><br>
  1. Weird 3d boxes rendering<br>
  2. Alpha also not working</p>
  
  <p><strong>Problematic output:</strong><br>
  <img src="https://u.cubeupload.com/testy/animation.gif" alt="img"></p>
  
  <p><strong>MWE (jupyter):</strong>  </p>
  
  <pre><code>%matplotlib inline
  import matplotlib.pyplot as plt
  import matplotlib.patches as patches
  from itertools import product
  from mpl_toolkits.mplot3d import Axes3D
  
  
  X , Y = [100,250], [0,100,200]
  xb, yb = 175, 125
  
  import pandas as pd
  matrix = np.array([
          [0.20, 0.10, 0.20],
          [0.05, 0.15, 0.30]
          ])
  df = pd.DataFrame(matrix, columns=Y)
  df.index = [100, 250]
  
  top = 1
  fig = plt.figure(figsize=(15,5))
  
  ax1 = fig.add_subplot(121)
  
  for xy in product(X,Y):
      x,y = xy[0], xy[1]
      z = df.loc[x,y]
      d1, d2 = xb - x, yb - y
      color = 'green' if d1*d2 &gt; 0 else 'red'
      ax1.add_patch(patches.Rectangle((x, y), d1, d2, alpha=z, facecolor=color))
      ax1.scatter(x,y,color='black')
  
  
  ax1.axvline(x=Xb, ls=':', color='blue')
  ax1.axhline(y=Yb, ls=':', color='blue')
  ax1.set_xticks(X)
  ax1.set_yticks(Y)
  ax1.set_xlim([min(X)-50,max(X)+50])
  ax1.set_ylim([min(Y)-50,max(Y)+50])
  
  
  
  ax2 = fig.add_subplot(122, projection='3d')
  ax2.view_init(elev=30., azim=-50)
  for xy in product(X,Y):    
      x ,y = xy[0], xy[1]
      z = df.loc[x,y]
  #     print(x, y, z)
      width = x - 175
      depth = y - 125
      pro = width*depth
      top = z
      bottom = np.zeros_like(top)
      if pro &gt; 0: #positive
          color='#B9F6CA'
      else:
          color='#EF9A9A'
      ax2.bar3d(x, y, bottom,  -width, -depth, top, color=color)
      ax2.scatter(x, y, z, color='blue')
  
  def rotate(angle):
      ax2.view_init(azim=angle)    
  
  from matplotlib import animation
  ani = animation.FuncAnimation(fig, rotate, frames=np.arange(0,362,2),interval=100)    
  from IPython.display import HTML
  plt.close()
  HTML(ani.to_jshtml())
  </code></pre>
  
  <p><strong>Related math problem:</strong><br>
  <a href="https://i.stack.imgur.com/oLDRg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oLDRg.png" alt="enter image description here"></a></p>