Matplotlib:点神秘地更改Alpha

时间:2018-10-11 19:56:54

标签: python numpy matplotlib jupyter-notebook ipython

我正在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 个答案:

答案 0 :(得分:1)

使用Axes3d.scatterdepthshade自变量

  

depthshade是否为散射标记着色以显示深度。默认值为True。

将此设置为False,以使绘图中的alpha不变。

答案 1 :(得分:0)

这不能解释什么使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())