Matplotlib: points changing alpha mysteriously

I'm working on a simple 3D animation in matplotlib within an IPython notebook, but my points are changing alpha values mysteriously:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
from IPython.display import HTML
import requests

%matplotlib inline

requests.get('https://gist.github.com/duhaime/023897b9bda70e7728c7db9792a11bd3/raw/b632e2ea9fb693f303908f546a684a3afcc329c0/data.npy')
X = np.load('data.npy')

def update_points(time, points):
  arr = np.array([[ X[time][i][0], X[time][i][1] ] for i in range(int(X.shape[1]))])
  points.set_offsets(arr) # set x, y values
  points.set_3d_properties(X[time][:,2][:], zdir='z') # set z value

def get_plot():
  fig = plt.figure()
  ax = p3.Axes3D(fig)
  ax.set_xlim(-10,10)
  ax.set_ylim(-10,10)
  ax.set_zlim(-10,10)
  points = ax.scatter(X[0][:,0][:], X[0][:,1][:], X[0][:,2][:]) # x,y,z vals
  return animation.FuncAnimation(fig,
    update_points,
    200,          # steps
    interval=100, # how often to refresh plot
    fargs=(points,),
    blit=False  
  ).to_jshtml()

HTML(get_plot())

Does anyone know why the points' alpha values are changing? Any suggestions others can offer would be very helpful!

2 answers

  • answered 2018-10-11 19:58 duhaime

    This doesn't explain what's mutating the alpha values, but one can mutate them back to 1 in the update function:

    import numpy as np
    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d.axes3d as p3
    import matplotlib.animation as animation
    from IPython.display import HTML
    import requests
    
    %matplotlib inline
    
    requests.get('https://gist.github.com/duhaime/023897b9bda70e7728c7db9792a11bd3/raw/b632e2ea9fb693f303908f546a684a3afcc329c0/data.npy')
    X = np.load('data.npy')
    
    def update_points(time, points):
      arr = np.array([[ X[time][i][0], X[time][i][1] ] for i in range(int(X.shape[1]))])
      points.set_offsets(arr) # set x, y values
      points.set_3d_properties(X[time][:,2][:], zdir='z') # set z value
      points.set_alpha(1)
    
    def get_plot(lim=3):
      fig = plt.figure()
      ax = p3.Axes3D(fig)
      ax.set_xlim(-lim, lim)
      ax.set_ylim(-lim, lim)
      ax.set_zlim(-lim, lim)
      points = ax.scatter(X[0][:,0][:], X[0][:,1][:], X[0][:,2][:]) # x,y,z vals
      return animation.FuncAnimation(fig,
        update_points,
        200,          # steps
        interval=100, # how often to refresh plot
        fargs=(points,),
        blit=False  
      ).to_jshtml()
    
    HTML(get_plot())
    

  • answered 2018-10-11 20:09 ImportanceOfBeingErnest

    Use the depthshade argument of Axes3d.scatter

    depthshade Whether or not to shade the scatter markers to give the appearance of depth. Default is True.

    Set this to False to have no alpha changes in your plot.