Skip to content

mplot3d failed to clip bar charts if zlim is set. #8902

Open

Description

@a113n

Bug report

Bug summary

mplot3d failed to clip bar charts if zlim is set.

Code for reproduction

# Adapter from http://matplotlib.org/2.0.2/examples/mplot3d/bars3d_demo.html
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
    xs = np.arange(20)
    ys = np.random.rand(20)

    # You can provide either a single color or an array. To demonstrate this,
    # the first bar of each set will be colored cyan.
    cs = [c] * len(xs)
    cs[0] = 'c'
    ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)

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

# Set zlim3d
ax.set_zlim3d(bottom=0.2, top=1)

plt.show()

Actual outcome

test

Expected outcome
The z-axis would be clipped according to the zlim_3d parameters properly

Matplotlib version

  • Operating System: Windows 10 1703
  • Matplotlib Version: 2.0.2
  • Python Version: 3.6
  • Jupyter Version (if applicable):
  • Other Libraries:

Matplotlib installed from pip.

Activity

  1. added this to the milestone on Jul 17, 2017
  2. tacaswell commented on Jul 17, 2017

    @tacaswell
    Member

    To my understanding we do not compute any sort of 3D intersections or have a proper z-buffer (either of which could be used to implement this) and both of which would be pretty major efforts to implement.

    @a113n are you interested in working on this?

  3. a113n commented on Jul 18, 2017

    @a113n
    Author

    @tacaswell instead of implementing code to compute 3D intersections or z-buffer, could zlim_3d clip the data properly just like the workaround below?

    # Adapted from http://matplotlib.org/2.0.2/examples/mplot3d/bars3d_demo.html
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.ticker as ticker
    import matplotlib.pyplot as plt
    import numpy as np
    
    
    # Funcformatter to simulate zlim_3d
    def major_formatter(x, pos):
        return "{:.1f}".format(x+0.2)
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
        xs = np.arange(20)
        ys = np.random.rand(20)
        
        # Simulate zlim_3d by subtracting the min limit
        ys -= 0.2
        ys[ys < 0] = 0
    
        # You can provide either a single color or an array. To demonstrate this,
        # the first bar of each set will be colored cyan.
        cs = [c] * len(xs)
        cs[0] = 'c'
        ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)
    
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    
    # Shift the tick labels up by 0.2
    ax.zaxis.set_major_formatter(ticker.FuncFormatter(major_formatter))
    
    plt.show()

    Result:
    test

  4. tacaswell commented on Jul 20, 2017

    @tacaswell
    Member

    That works for this case where you know the shift and use it to both adjust the data and the tick labels. However I don't see how to generalize it.

    It would also be odd if it only worked in the z-direction, not x and y.

  5. QuLogic commented on Jun 23, 2018

    @QuLogic
    Member

    The code passes zdir='y' which is why the layering is in the y direction instead of z.

  6. modified the milestones: , needs sorting on Oct 6, 2022
  7. github-actions commented on Oct 9, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  8. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Oct 11, 2023
  9. scottshambaugh commented on Nov 28, 2023

    @scottshambaugh
    Contributor

    There is some discussion about this desired behavior in #25804

    #27349 will implement option B from the graphic below, but option C is really the thrust of the original question here.
    image

    Unfortunately, implementing that would require a huge rewrite of the 3D renderer to do proper culling with a z-buffer or 3D intersections, as @tacaswell mentioned in his comment. I don't think there's anyone able to do that right now, though if someone wants to take it on as a project it'd be awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Hardhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesNew featurekeepItems to be ignored by the “Stale” Github Actiontopic: mplot3d

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions