T. L
T. L

Reputation: 105

How to plot over time with each time being a different shade of the same colour

So basically I have a graph which plots the solution to my code every 10 timesteps, but I've realised it's impossible to tell which time is which step unless you already know what it's supposed to look like. So I had the idea of each time being a different shade of blue for B and and red for M, getting progressively darker for each timestep - does anyone know how I could do this? Thanks!

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

Upvotes: 1

Views: 539

Answers (1)

iury simoes-sousa
iury simoes-sousa

Reputation: 1590

You can do something like this:

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])

Which will return this figure: Figure

If you need a color reference you can create two color bars (each plot).

Upvotes: 1

Related Questions