gpt4 book ai didi

python - 在 matplotlib python 中以 3D 形式动画绘制两种不同的颜色和形状

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:56 25 4
gpt4 key购买 nike

我尝试为两个不同的类“红点”和“蓝色三角形”设置动画。一开始它们都在 x-y 平面上,并且在动画中它们都应该沿着 z 轴移动。最后它应该如图所示。

enter image description here

我用两个单独的数字达到了我想要的效果

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

np.random.seed(19680801)
n = 200
breite=20

def randrange(n, vmin, vmax):
'''
Helper function to make an array of random numbers having shape (n, )
with each number distributed Uniform(vmin, vmax).
'''
return (vmax - vmin)*np.random.rand(n) + vmin

def fun_ebene(x, y):
return x+y-breite**2/2

#function for points
def fun_punkte(x,y):
return -x**2

# min and max of the plane
maximum=-99999.9
minimum=99999.9
for x in randrange(n, -breite, breite):
for y in randrange(n, -breite, breite):
maximum=max(maximum,fun_ebene(x,y))
minimum=min(minimum,fun_ebene(x,y))

# cache that the points not touch the plane
cache=(maximum-minimum)/10

#generate points
xs = randrange(n, -breite, breite)
ys = randrange(n, -breite, breite)
zs = randrange(n, fun_punkte(xs,ys)+cache, maximum)
zs=fun_punkte(xs,ys)
axlistxs = []
axlistys = []
axlistzs = []
bxlistxs = []
bxlistys = []
bxlistzs = []

#seperates Data in two lists
for i in range(len(zs)):
if zs[i]<=fun_ebene(xs[i],ys[i]):
axlistxs.append(xs[i])
axlistys.append(ys[i])
axlistzs.append(zs[i])
else:
bxlistxs.append(xs[i])
bxlistys.append(ys[i])
bxlistzs.append(zs[i])

fig = plt.figure()
fig2 = plt.figure()
ax = fig.add_subplot(111, projection='3d')
bx = fig2.add_subplot(111, projection='3d')

#set limes for the coordinate system
ax.set_xlim(-22,22)
ax.set_ylim(-22,22)
ax.set_zlim(0, -400)
bx.set_xlim(-22,22)
bx.set_ylim(-22,22)
bx.set_zlim(0, -400)

def _update_plot(i, fig, scat):
axlistzs = []
# sets the z-coordinate +1 every frame
for date in range(0, len(axlistxs)):
axlistzs.append(-i)
list1 =[]
list1.append(axlistxs) #x-coordinates
list1.append(axlistys) #y-coordinates
list1.append(axlistzs) #z-coordinates
scat._offsets3d = (*np.array(list1),)
return scat

def _update_plot2(i, fig, scat2):
bxlistzs = []
for data in range(0, len(bxlistxs)):
bxlistzs.append(-i)
list2 = []
list2.append(bxlistxs) #x-coordinates
list2.append(bxlistys) #y-coordinates
list2.append(bxlistzs) #z-coordinates
np.array(list2)
scat2._offsets3d = (*np.array(list2),)
return scat2

#plot data
scat = ax.scatter([], [], color='red', marker='o')
scat2 = bx.scatter([], [], color = 'blue', marker = '^')

anim = animation.FuncAnimation(fig, _update_plot, fargs=(fig, scat),
frames=100, interval=100)

anim2 = animation.FuncAnimation(fig2, _update_plot2, fargs=(fig2, scat2),
frames=100, interval=100)

plt.show()

我的问题是我只想在一个图形/动画中实现所有这些。但我不知道如何分散两种不同的颜色和形状。在我使用的更新函数中:

scat._offsets3d = (*np.array(list1),)

更新多个 x.y.z 坐标的列表,之后数据分散

scat = ax.scatter([], [], color='red', marker='o') 

我发现不可能在一张图中以不同的颜色和形状绘制两个不同的类列表。我希望有人能提供帮助。

最佳答案

正如评论中反复提到的,您需要从代码中删除第二个数字。然后,您也只需要一个 FuncAnimation 和一个更新函数。我保留了其余代码不变(尽管也可能有优化的空间),以便您看到这实际上只是从脚本中删除第二个图形及其所有依赖项。

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

np.random.seed(19680801)
n = 200
breite=20

def randrange(n, vmin, vmax):
'''
Helper function to make an array of random numbers having shape (n, )
with each number distributed Uniform(vmin, vmax).
'''
return (vmax - vmin)*np.random.rand(n) + vmin

def fun_ebene(x, y):
return x+y-breite**2/2

#function for points
def fun_punkte(x,y):
return -x**2

# min and max of the plane
maximum=-99999.9
minimum=99999.9
for x in randrange(n, -breite, breite):
for y in randrange(n, -breite, breite):
maximum=max(maximum,fun_ebene(x,y))
minimum=min(minimum,fun_ebene(x,y))

# cache that the points not touch the plane
cache=(maximum-minimum)/10

#generate points
xs = randrange(n, -breite, breite)
ys = randrange(n, -breite, breite)
zs = randrange(n, fun_punkte(xs,ys)+cache, maximum)
zs=fun_punkte(xs,ys)
axlistxs = []
axlistys = []
axlistzs = []
bxlistxs = []
bxlistys = []
bxlistzs = []

#seperates Data in two lists
for i in range(len(zs)):
if zs[i]<=fun_ebene(xs[i],ys[i]):
axlistxs.append(xs[i])
axlistys.append(ys[i])
axlistzs.append(zs[i])
else:
bxlistxs.append(xs[i])
bxlistys.append(ys[i])
bxlistzs.append(zs[i])

# only one figure and one axes
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')


ax.set_xlim(-22,22)
ax.set_ylim(-22,22)
ax.set_zlim(0, -400)

# only one updating function
def _update_plot(i):
axlistzs = []
# sets the z-coordinate +1 every frame
for date in range(0, len(axlistxs)):
axlistzs.append(-i)
scat._offsets3d = (axlistxs,axlistys,axlistzs)

bxlistzs = []
for data in range(0, len(bxlistxs)):
bxlistzs.append(-i)
scat2._offsets3d = (bxlistxs,bxlistys,bxlistzs)

#plot data to the same axes
scat = ax.scatter([], [], color='red', marker='o')
scat2 = ax.scatter([], [], color = 'blue', marker = '^')

# only one FuncAnimation
anim = animation.FuncAnimation(fig, _update_plot,
frames=100, interval=100)


plt.show()

关于python - 在 matplotlib python 中以 3D 形式动画绘制两种不同的颜色和形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47562213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com