Skip to content

index in pick_event invalid and inconsistent for mplot3d scatter plot (z ordering?) #19735

Open

Description

@saasbk

Bug report

Bug summary

The index returned in the pick_event (event.ind) does not correspond to the index of the source data.
It also changes depending on the view onto the data.

The example contains two points.
When you click on a point, the event.ind is printed out.
The one at (0,0,0) should have index 0, the other index 1.

Rotate the plot so that the second point is further behind (semi transparent). Now it has index 0.
Rotate the plot so that the second point is further in front (full opacity). Now it has index 1.

Code for reproduction

`

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


if __name__ == "__main__":
    fig = plt.figure(constrained_layout=True, figsize=(6,6))

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

    num_points = 2
    data = np.zeros([num_points,3])
    data[:,0] = np.linspace(1,0,num_points)
    print(data)

    path_collection = ax.scatter(xs=data[:,0],
                                 ys=data[:,1],
                                 zs=data[:,2],
                                 picker=True)

    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_zlabel("z")
    ax.set_xlim(-1,1)
    ax.set_ylim(-1,1)
    ax.set_zlim(-1,1)

    def onpick(event):
        print(event.ind)

    fig.canvas.mpl_connect('pick_event', onpick)

    plt.show()`

Actual outcome

The example contains two points.
When you click on a point, the event.ind is printed out.
The one at (0,0,0) should have index 0, the other index 1.

Rotate the plot so that the second point is further behind (semi transparent). Now it has index 0.
Rotate the plot so that the second point is further in front (full opacity). Now it has index 1.

Expected outcome

The returned event.ind should not depend on view.
For the point in the centre it should always return 0, for the point to the right, it should always return 1.

Matplotlib version

  • Operating system: macOS Catalina; 10.15.4
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): '3.3.4'
  • Matplotlib backend (print(matplotlib.get_backend())): 'MacOSX'
  • Python version: Python 3.6.9
  • Jupyter version (if applicable):
  • Other libraries:

Activity

  1. snaxxus commented on Mar 31, 2021

    @snaxxus

    The axes reverse directions on the selection depending on which points are in front when you rotate the graph. Using matplotlib 3.4.1

    #!/usr/bin/env python3
    
    from mpl_toolkits.mplot3d import proj3d
    import matplotlib.pyplot as plt
    
    import matplotlib
    print('matplotlib: {}'.format(matplotlib.__version__))
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection = '3d')
    
    x = [0, 2, 0,0]
    y = [0, 2, 0,2]
    z = [0, 2, 2,0]
    
    scatter = ax.scatter(x,y,z,picker=True)
    
    def chaos_onclick(event):
        point_index = int(event.ind)
        print(point_index)
    
        print("X=",x[point_index], " Y=",y[point_index], " Z=",z[point_index], " PointIdx=", point_index)
    
    fig.canvas.mpl_connect('pick_event', chaos_onclick)
    plt.show()
    
  2. snaxxus commented on Apr 1, 2021

    @snaxxus

    Anyone got a clue where the code is that populates the event.ind? I took linear algebra and I write python code all the time, but finding my way around this project, where should I start?

  3. QuLogic commented on Apr 2, 2021

    @QuLogic
    Member

    That's determined in Collection.contains; it's likely that there needs to be an override in Path3DCollection to undo the effect of depth sorting.

  4. snaxxus commented on Apr 2, 2021

    @snaxxus

    I'll take a look. meanwhile this is a work around.

    #!/usr/bin/env python3
    
    # much from https://stackoverflow.com/questions/58218482/annotate-3d-scatter-plot-on-pick-event/66926265#66926265
    
    from mpl_toolkits.mplot3d import proj3d
    import matplotlib.pyplot as plt
    import numpy as np
    
    import matplotlib
    print('matplotlib: {}'.format(matplotlib.__version__))
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection = '3d')
    
    x = [0, 2, 0,0]
    y = [0, 2, 0,2]
    z = [0, 2, 2,0]
    
    scatter = ax.scatter(x,y,z,picker=True)
    
    ax.set_xlabel('X axis')
    ax.set_ylabel('Y axis')
    ax.set_zlabel('Z axis')
    
    
    def onMouseMotion(event):
      print(event)
    
    def chaos_onclick(event):
    
        print(dir(event.mouseevent))
        xx=event.mouseevent.x
        yy=event.mouseevent.y
        
        #magic from https://stackoverflow.com/questions/10374930/matplotlib-annotating-a-3d-scatter-plot
        x2, y2, z2=proj3d.proj_transform(x[0], y[0], z[0], plt.gca().get_proj())
        x3, y3 = ax.transData.transform((x2, y2))
        #the distance
        d=np.sqrt ((x3 - xx)**2 + (y3 - yy)**2)
        
        print ("distance=",d)
        
        
        #find the closest by searching for min distance.
        #All glory to https://stackoverflow.com/questions/10374930/matplotlib-annotating-a-3d-scatter-plot
        imin=0
        dmin=10000000
        for i in range(len(x)):
          #magic from https://stackoverflow.com/questions/10374930/matplotlib-annotating-a-3d-scatter-plot
          x2, y2, z2=proj3d.proj_transform(x[i], y[i], z[i], plt.gca().get_proj())
          x3, y3 = ax.transData.transform((x2, y2))
          #the distance magic from https://stackoverflow.com/questions/10374930/matplotlib-annotating-a-3d-scatter-plot
          d=np.sqrt ((x3 - xx)**2 + (y3 - yy)**2)
          #We find the distance and also the index for the closest datapoint
          if d< dmin:
            dmin=d
            imin=i
            
          #print ("i=",i," d=",d, " imin=",imin, " dmin=",dmin)
        
        # gives the incorrect data point index
        point_index = int(event.ind)
    
        print("Xfixed=",x[imin], " Yfixed=",y[imin], " Zfixed=",z[imin], " PointIdxFixed=", imin)
        print("Xbroke=",x[point_index], " Ybroke=",y[point_index], " Zbroke=",z[point_index], " PointIdx=", point_index)
    
    fig.canvas.mpl_connect('pick_event', chaos_onclick)
    #fig.canvas.mpl_connect('motion_notify_event', onMouseMotion)  # on mouse motion
    
    
    plt.show()
    
    
  5. added this to the v3.5.0 milestone on Apr 2, 2021
  6. tacaswell commented on Apr 2, 2021

    @tacaswell
    Member

    On the bright side, via #19812 we now carry the z-sorting on the artist so we know enough to do the inversion.

  7. modified the milestones: v3.5.0, v3.6.0 on Sep 25, 2021
  8. modified the milestones: v3.6.0, v3.7.0 on Jul 8, 2022
  9. modified the milestones: v3.7.0, v3.7.1 on Feb 14, 2023
  10. modified the milestones: v3.7.1, v3.7.2 on Mar 4, 2023
  11. modified the milestones: v3.7.2, v3.7.3 on Jul 5, 2023
  12. modified the milestones: v3.7.3, v3.8.0 on Sep 9, 2023
  13. removed this from the v3.8.0 milestone on Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions