Thanks to visit codestin.com
Credit goes to github.com

Skip to content

3D bar graphs with variable depth #569

@kosma

Description

@kosma

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'])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions