代码之家  ›  专栏  ›  技术社区  ›  duhaime

Matplotlib:点神秘地改变alpha

  •  0
  • duhaime  · 技术社区  · 7 年前

    我正在IPython笔记本的matplotlib中制作一个简单的3D动画,但我的观点正在神秘地改变alpha值:

    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())
    

    有人知道为什么点的alpha值在变化吗?其他人能提供的任何建议都会很有帮助!

    2 回复  |  直到 7 年前
        1
  •  1
  •   ImportanceOfBeingErnest    7 年前

    使用 depthshade 的论点 Axes3d.scatter

    将此设置为 False 在你的图中没有alpha变化。

        2
  •  0
  •   duhaime    7 年前

    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())