我试图将三个复杂的函数(不是它们的实,模或虚部)绘制在一起。我的代码显示了这些图,但是图例中有重复的标签。请任何人帮我,我将不胜感激。这是我的代码:
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
#from mpl_toolkits.mplot3d import Axes3D
#%% Parameters
Nx = 150 #% number of cells
Nt=20
a=0.001 #-1 0
b=pi #1
c=pi
lbda_r = 0.0 #reactivity
Pe01=0.012
Pe25=0.25
Pe5=0.5
teata =0.5 # Implicit Euler = 1 C Nicholson = 0.5 Explicit Euler = 0
APe01 = str(Pe01)
PP01=APe01.partition('.')[2]
APe25 = str(Pe25)
PP25=APe25.partition('.')[2]
APe5 = str(Pe5)
PP5=APe5.partition('.')[2]
BLB = str(lbda_r)
PPlb=BLB.partition('.')[2]
if lbda_r < 0:
lbda = 'neg' + PPlb
elif lbda_r == 0:
lbda = PPlb
else:
lbda = 'pos' + PPlb
if 0 < teata < 1:
tea = str(teata)
TTA = tea.partition('.')[2]
else:
TTA = str(teata)
#========================================================================
# The DOMAIN is DISCRETIZED below.
#========================================================================
khh=linspace(a,c,Nx)
Cf=linspace(a,b,Nx)
Cfl,kh=np.meshgrid(khh,Cf)
#print kh
dkh=khh[2]-khh[1]
#==================================================================
# IMPLEMENT NUMERICAL EXPERIMENTS
#=======================================================================
U_0 = 1 #inital amplitude
def Ana_solution(Pe): #Analytical solution function
return U_0*exp(Nt*(lbda_r -Pe*(kh)**2-Cfl*kh*1j))
def Theta_Solu(Pe,teata): #Theta solution function
P_o=-Pe*(kh)**2 + lbda_r
A_tea = 1./((1-teata*P_o)**2 + (Cfl*kh)**2)
G_OR = (1-teata*P_o)*(1 + (1-teata)*P_o) + teata*(1-teata)*(Cfl*kh)**2
G_OI = -Cfl*kh
return U_0*(A_tea *(G_OR + G_OI*1j))**Nt
def DIRK2_Solu(Pe): #DIRK2 Solution function
alpha=1 + sqrt(2)/2
A = (2*alpha -1)/alpha
B =(1-alpha)/alpha
P_o=-Pe*(kh)**2 + lbda_r
A_dir = 1./((1-alpha*P_o)**2 + Cfl*(kh)**2)**2
G_OR = A*((1-alpha*P_o)**3 + (1-alpha*P_o)*(Cfl*kh)**2) + B*(1-alpha*P_o)**2 -(alpha*Cfl*kh)**2
G_OI = -(alpha*A*(Cfl*kh*(1-alpha*P_o)**2 + (Cfl*kh)**3) + 2*alpha*B*Cfl*kh*(1-alpha*P_o))
return U_0*(A_dir *(G_OR + G_OI*1j))**Nt
U_ana = Ana_solution(Pe01) #analytical solution
U_tea05 = Theta_Solu(Pe01,teata) #C-Nicholson solution
U_tea0 = Theta_Solu(Pe01,0) #Forward Euler solution
U_tea1 = Theta_Solu(Pe01,1) #Backward Euler solution
U_Dirk2 =DIRK2_Solu(Pe01) #DIRK2 Solution
THETA={0.5:'Crank-Nicholson',1:'Backward Euler', 0:'Forward Euler'}
figure(1)
plot( kh,U_ana[:,9],'-',label = 'Analytical solution')
plot( kh,U_tea0[:,9],'H',label = 'Forward Euler solution')
plot( kh,U_tea1[:,9],'v',label ='Backward Euler solution')
legend()
title('Numerical solution versus analytical solution.')
xlabel('$ C_f $')
ylabel(' $\mathcal{U}$')
#savefig('/media/amikiya/BABC-276E/BackupHp/PHD/Thesis/Manuscript /FEBE_Sol_kh_LB'+str(lbda)+'Pe'+str(PP01)+'.png',bbox_inches='tight') # save the figure to a file
#close(1)
show(1)
figure(2)
plot( kh,U_ana[:,9],'-',label = 'Analytical solution')
plot( kh,U_tea05[:,9],'*',label = str(THETA[teata])+' solution')
plot( kh,U_Dirk2[:,9],'+',label = 'DIRK2 solution')
legend()
title('Numerical solution versus analytical solution.')
xlabel('$ C_f $')
ylabel(' $\mathcal{U}$')
#savefig('/media/amikiya/BABC-276E/BackupHp/PHD/Thesis/Manuscript/THEDIR_Sol_CF_LB'+str(lbda)+'Pe'+str(PP01)+'.png',bbox_inches='tight') # save the figure to file
#close(2)
show(2)
figure(3)
plot( kh,U_ana[9,:],'-',label = 'Analytical solution')
plot( kh,U_tea0[9,:],'H',label = 'Forward Euler solution')
plot( kh,U_tea1[9,:],'v',label ='Backward Euler solution')
legend()
title('Numerical solution versus analytical solution.')
xlabel('$ \kappa \Delta x $')
ylabel(' $\mathcal{U}$')
#close(3)
show(3)
Theata=str(THETA[teata]) + ' solution'
figure(4)
PUana=plt.plot( kh,U_ana[9,:],'-',label = 'Analytical solution')
PUnum_T =plt.plot( kh,U_tea05[9,:],marker ='*',label = Theata)
PUnum_D =plt.plot( kh,U_Dirk2[9,:],marker='+',label = 'DIRK2 solution')
legend([PUana,PUnum_T,PUnum_D])
title('Numerical solution versus analytical solution.')
xlabel('$ \kappa \Delta x $')
ylabel(' $\mathcal{U}$')
show(4)
figure(5)
PUana, =plt.plot( kh,real(U_ana[9,:]),label = 'Analytical solution')
PUnum_T, =plt.plot( kh,real(U_tea05[9,:]),label = Theata)
PUnum_D, =plt.plot( kh,real(U_Dirk2[9,:]),label = 'DIRK2 solution')
legend((PUana,PUnum_T,PUnum_D),('Analytical solution',Theata,'DIRK2 solution'))
title('Numerical solution versus analytical solution.')
xlabel('$ \kappa \Delta x $')
ylabel(' $\mathcal{U}$')
show(5)