python 散点图动_在散点图中设置点移动动画(matplotlib python)

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D, proj3d

import matplotlib.animation as animation

N_points = 10

def update(num, my_ax):

# the following corresponds to whatever logic must append in your code

# to get the new coordinates of your points

# in this case, we're going to move each point by a quantity (dx,dy,dz)

dx, dy, dz = np.random.normal(size=(3,N_points), loc=0, scale=1)

debug_text.set_text("{:d}".format(num)) # for debugging

x,y,z = graph._offsets3d

new_x, new_y, new_z = (x+dx, y+dy, z+dz)

graph._offsets3d = (new_x, new_y, new_z)

for t, new_x_i, new_y_i, new_z_i in zip(annots, new_x, new_y, new_z):

# animating Text in 3D proved to be tricky. Tip of the hat to @ImportanceOfBeingErnest

# for this answer https://stackoverflow.com/a/51579878/1356000

x_, y_, _ = proj3d.proj_transform(new_x_i, new_y_i, new_z_i, my_ax.get_proj())

t.set_position((x_,y_))

return [graph,debug_text]+annots

# create N_points initial points

x,y,z = np.random.normal(size=(3,N_points), loc=0, scale=10)

fig = plt.figure(figsize=(5, 5))

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

graph = ax.scatter(x, y, z, color='orange')

debug_text = fig.text(0, 1, "TEXT", va='top') # for debugging

annots = [ax.text2D(0,0,"POINT") for _ in range(N_points)]

# Creating the Animation object

ani = animation.FuncAnimation(fig, update, fargs=[ax], frames=100, interval=50, blit=True)

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值