matplotlib/matplotlib — Issue #569: 3D bar graphs with variable depth
Author: kosma
Created: 2011-11-08 12:28:23+00:00
State: open

When attempting to plot a 3D bar graph with the bars having different depths, they are not rendered correctly. Sample code:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
try:
ax = fig.add_subplot(111, projection='3d')
except:
ax = Axes3D(fig)
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
xs = np.arange(20)
ys = np.random.rand(20)
zs = [z] * len(xs)
# stagger the first bar a bit - causes rendering problems
zs[0] += 5

cs = [c] * len(xs)

ax.bar(xs, ys, zs=zs, zdir='y', color=cs, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

Fix:

diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py
index 65be58e..87fdc2b 100644
--- a/lib/mpl_toolkits/mplot3d/axes3d.py
+++ b/lib/mpl_toolkits/mplot3d/axes3d.py
@@ -1916,7 +1916,7 @@ class Axes3D(Axes):
vs = art3d.get_patch_verts(p)
verts += vs.tolist()
verts_zs += [z] * len(vs)
- art3d.patch_2d_to_3d(p, zs, zdir)
+ art3d.patch_2d_to_3d(p, z, zdir)
if 'alpha' in kwargs:
p.set_alpha(kwargs['alpha'])