我做了一个animation of a wave,但是颜色条不断移动,我不知道为什么。我认为这是因为它在每帧之后都会正常化。如何设置色彩图设置以使色条保持不变?
代码:警告:此prgramm删除调用目录中的文件夹png3D。根据您的计算能力,所有帧都可能需要一段时间。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import time
import sys,os,shutil
time.clock()
def FUNCTION(p,r,t):
k_0,dx,c = p
x,y = r
x,y = np.meshgrid(x,y)
r = np.sqrt(x**2 + y**2)
z = np.exp(1j*(k_0[0]*x+k_0[1]*y-c*t))*np.exp(-((r-c*t)/(2*dx))**2 )*0.5**(r/x[0][-1]/2)
z = abs(z)
return(x,y,z)
def PRINT_PNG(x,y,t,folder):
x,y,z = FUNCTION(p,r,t)
fig = plt.figure()
sub = fig.gca(projection='3d')
fig.suptitle("3D Wavepackage", size="large")
sub.set_zlim([0,1.5])
sub.set_xlabel("x",size="large")
sub.set_ylabel("y",size="large")
sub.set_zlabel("$Re[\psi(x,t)]$",size="large",ha="right")
sub.text(0.5,0.5,0.5,"t = "+str("%3.1f"%t)+" s",transform = sub.transAxes, ha="right")
surf = sub.plot_surface(x,y,z,linewidth=0,cmap=cm.coolwarm)
fig.colorbar(surf, shrink=0.5, aspect=5)
file_name = os.path.abspath(folder+"/tmp"+str("%04d"%i)+".png")
fig.savefig(file_name)
plt.close()
# Parameters
T = 10
N = 100
n = 24*T # 24 Frames pro Sekunde
t = np.linspace(0,T,n)
k_0 = [1,1]
dx = 1
c = 1
p = [k_0,dx,c]
x = np.linspace(-c*T,c*T,N)
y = np.linspace(-c*T,c*T,N)
r=[x,y]
# Baue Ordner
folder = os.path.abspath("png3D")
if os.path.exists(folder)==True:
shutil.rmtree(folder)
os.makedirs(folder)
else:
os.makedirs(folder)
for i in range(n):
PRINT_PNG(x,y,t[i],folder)
print(str(i+1)+"/"+str(n))
file_name = os.path.abspath("png3D//tmp%04d.png")
os.system("ffmpeg -f image2 -y -i "+file_name+" -r 24 -bit_rate 1800 3D_Wave.mpeg")
shutil.rmtree(folder)
print(time.clock())
答案 0 :(得分:3)
您可以将vmin
和vmax
个关键字参数传递给指定图表值范围的plot_surface
:
surf = sub.plot_surface(x,y,z,linewidth=0,cmap=cm.coolwarm,vmin=0,vmax=1)
有关详细信息,请参阅the docs。