Skip to content

Calling invert_yaxis() on a 3D plot has either no effect or removes ticks #14577

Closed

Description

@ffaallkk

Bug report

Bug summary

Using version 3.1.0, ax.invert_yaxis() does not work as expected when called on a 3D scatter plot. There seems to be no effect when it is called after populating the plot, while calling it before the plot inverts the data points correctly but moves all ticks to one point.

Code for reproduction

# Version 1: calling invert_yaxis() after populating the plot.
# Version 2 (omitted): calling invert_yaxis() after ax = fig.add_subplot(111, projection='3d')
# Using the official 3D scatter plot example. 

from mpl_toolkits.mplot3d import Axes3D 

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)

def randrange(n, vmin, vmax):
    '''
    Helper function to make an array of random numbers having shape (n, )
    with each number distributed Uniform(vmin, vmax).
    '''
    return (vmax - vmin)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

n = 100

# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zlow, zhigh)
    ax.scatter(xs, ys, zs, marker=m)

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

plt.show()

Actual outcome when inverting axis after populating plot:

Figure_0

Actual outcome when inverting axis before populating plot:

Figure_1

Expected outcome

Using ax.set_ylim(105,-5):

Figure_2

Matplotlib version

  • Operating system: Ubuntu 18.04.2
  • Matplotlib version: 3.1.0
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.6.8
  • Jupyter version (if applicable):
  • Other libraries: Numpy 1.16.1

Matplotlib and Python were installed with pip.


Also posted on SO, https://stackoverflow.com/questions/56666916/inverting-axis-of-matplotlib-3d-plot-ruins-ticks

Activity

  1. ImportanceOfBeingErnest commented on Jun 19, 2019

    @ImportanceOfBeingErnest
    Member

    Bisects to #13330 (No surprise, a feature was added without tests!)


    A slightly better test case

    import matplotlib
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    
    fig = plt.figure(figsize=(9,3.5), dpi=80)
    ax1 = fig.add_subplot(121, projection='3d')
    ax2 = fig.add_subplot(122, projection='3d')
    fig.suptitle(f"matplotlib v{matplotlib.__version__}")
    
    #First plot, then invert
    ax1.plot([1,1,10,10], [1,10,10,10], [1,1,1,10], marker="o")
    ax1.invert_yaxis()
    
    #First invert, then plot
    ax2.invert_yaxis()
    ax2.plot([1,1,10,10], [1,10,10,10], [1,1,1,10], marker="o")
    
    plt.show()
    

    Actual output

    image

    First plotting, then inverting the axes (left) just does nothing. First inverting, then plotting (right) scrambles the ticks.

    Expected output

    same as in matplotlib 3.0.2:

    image

  2. added
    Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
    on Jun 19, 2019
  3. baloe commented on Dec 2, 2021

    @baloe

    Still not the expected output for me:
    Figure2

  4. qizidog commented on Jan 4, 2022

    @qizidog

    Bisects to #13330 (No surprise, a feature was added without tests!)

    A slightly better test case

    import matplotlib
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    
    fig = plt.figure(figsize=(9,3.5), dpi=80)
    ax1 = fig.add_subplot(121, projection='3d')
    ax2 = fig.add_subplot(122, projection='3d')
    fig.suptitle(f"matplotlib v{matplotlib.__version__}")
    
    #First plot, then invert
    ax1.plot([1,1,10,10], [1,10,10,10], [1,1,1,10], marker="o")
    ax1.invert_yaxis()
    
    #First invert, then plot
    ax2.invert_yaxis()
    ax2.plot([1,1,10,10], [1,10,10,10], [1,1,1,10], marker="o")
    
    plt.show()
    

    Actual output

    image

    First plotting, then inverting the axes (left) just does nothing. First inverting, then plotting (right) scrambles the ticks.

    Expected output

    same as in matplotlib 3.0.2:

    image

    The same code output in macos m1. There is still a fatal problem.

    image.png

  5. ivanov commented on Jan 5, 2022

    @ivanov
    Member

    @qizidog I think this ticket is old enough and has been closed long enough that your comment is unlikely to get noticed acted upon. @baloe created a new ticket (#21843) and that closed as duplicate of #21369. So please follow up there, if it makes sense, or open a new ticket.

  6. HabibSlim commented on Nov 7, 2022

    @HabibSlim

    This issue still exists in 3.2.2. I'm aware that it might be non-trivial to fix but it's a pretty big bug

  7. jklymak commented on Nov 7, 2022

    @jklymak
    Member

    This was supposed to have been fixed in 3.6.0. If it is not fixed please open a new issue with required information.

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

    Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.topic: mplot3d

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions