如何随着时间变化而每次绘制为相同颜色的不同阴影

时间:2018-12-13 16:42:42

标签: python matplotlib plot

因此,基本上,我有一个图形,每10个时间步绘制代码解决方案,但是我已经意识到,除非您已经知道它应该是什么样子,否则不可能知道哪个时间是哪一步。所以我想到了每次B的蓝色和M的红色都是不同的阴影,每个时间步长都逐渐变暗-有人知道我该怎么做吗?谢谢!

import numpy as np 
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import random
from math import sqrt
%matplotlib inline

r1=0.1 # growth rate B
r2=0.3 # growth rate M
KB=20 # carrying capacity B
x=np.arange(0,70) # position
dx=1 # distance step
m=71 # number of distance steps
alpha =  0.00002 # predation rate of M on B
beta=0 # growth rate M from eating B
KM=71*[] # carrying capacity M
KM[0:71]=4000+(4000/2)*(1+np.cos(np.pi*x/35))
n=101 # years
dt=1 # time step

B=np.zeros(shape=(m,n)) # B
M=np.zeros(shape=(m,n)) # M
D=0.35 # diffusivity of B
D2=0.05 # diffusivity of M
Alpha=(D*dt)/(dx*dx) # diffusion term for the B
Alpha2=(D2*dt)/(dx*dx) # diffusion term for the M

M[0,0]=0
M[m-1,0]=0
B[1:m-1,0]=5
M[1:26,0]=2500
M[26:44,0]=1000 # initial conditions
M[44:m-1,0]=2500
for k in range(0,n-1):
    B[0,k+1]=B[1,k+1]
    B[m-1,k+1]=B[m-2,k+1] # boundary conditions
    M[0,k+1]=0
    M[m-1,k+1]=0
    for i in range(1,m-1):
        B[i,k+1]=(1-2*Alpha)*B[i,k]+Alpha*B[i+1,k]+Alpha*B[i-1,k]+r1*B[i,k]*(1-B[i,k]/KB)-alpha*M[i,k]*B[i,k] # 
        M[i,k+1]=(1-2*Alpha2)*M[i,k]+Alpha2*M[i+1,k]+Alpha2*M[i-1,k]+r2*M[i,k]*(1-M[i,k]/KM[i])+beta*M[i,k]*B[i,k]
    if (k+1) % 10 == 0 or k==0:
        plt.plot(B[:,k],color='b') 
        plt.plot(M[:,k]/1000,color='r')

plt.xlabel('Position')
plt.ylabel('Density per 10 ha')
plt.title('Distributions of the B and M population every 10 years \n - with equilibrium distribution of B')
speciesM = mpatches.Patch(color='r', label='M (1000)')
speciesB = mpatches.Patch(color='b', label='B')
plt.legend(handles=[speciesM,speciesB])

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

colors = {
    'blues':plt.cm.Blues(np.linspace(0.1,1,B.shape[0])),
    'reds':plt.cm.Reds(np.linspace(0.1,1,M.shape[0]))
}

fig,ax = plt.subplots()
for k in range(B.shape[0]):
    ax.plot(B[:,k],color=colors['blues'][k])
    ax.plot(M[:,k]/1000,color=colors['reds'][k])

将返回以下数字: Figure

如果需要颜色参考,可以创建两个颜色条(每个图)。