动画在matplotlib python中绘制两种不同的颜色和形状

时间:2017-11-29 21:35:01

标签: python animation matplotlib plot 3d

我尝试动画两个不同的类"红点"和"蓝色三角形"。在开始时它们都在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') 

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

1 个答案:

答案 0 :(得分:1)

正如评论中反复提到的那样,您需要从代码中删除第二个数字。然后,您还只需要一个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()