编程语言
首页 > 编程语言> > Python Matplotlib Colorbar / Colormap设置

Python Matplotlib Colorbar / Colormap设置

作者:互联网

我的数值是animation of a wave,但是色标一直在变化,我不知道为什么.我认为这是因为它在每一帧之后都正常化了.如何设置颜色图设置,使颜色栏保持不变?

代码:警告:此程序删除其调用目录中的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())

解决方法:

您可以将vmin和vmax关键字参数传递给plot_surface,以指定绘图的值范围:

surf = sub.plot_surface(x,y,z,linewidth=0,cmap=cm.coolwarm,vmin=0,vmax=1)

有关更多信息,请参见the docs.

标签:colorbar,matplotlib,python
来源: https://codeday.me/bug/20191122/2063790.html