【问题标题】:Matplotlib: points changing alpha mysteriouslyMatplotlib:点神秘地改变 alpha
【发布时间】:2018-10-11 19:56:54
【问题描述】:

我正在 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 值会发生变化?其他人可以提供的任何建议都会非常有帮助!

【问题讨论】:

    标签: python numpy matplotlib jupyter-notebook ipython


    【解决方案1】:

    使用Axes3d.scatterdepthshade 参数

    depthshade 是否对散射标记进行着色以显示深度。默认为真。

    将此设置为 False 以使您的绘图中没有 alpha 变化。

    【讨论】:

    • 阿门,谢谢@ImportanceOfBeingErnest! (欧内斯特是我的中间名)
    【解决方案2】:

    这并不能解释是什么改变了 alpha 值,但可以在更新函数中将它们变回 1:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      相关资源
      最近更新 更多