如何绕x和y轴切换?

时间:2018-12-11 14:33:24

标签: python numpy matplotlib plot

有了这个图,除了在轴上以错误的方式绘制数据这一事实之外,其他所有方法都是正确的-是否有一种简单的方法可以切换它们而无需返回所有数组并手动更改值?很长的代码很抱歉,其中大部分只是定义参数。谢谢!

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

r1=0.01 # growth rate B
r2=0.03 # growth rate M
KB = 20 # carrying capacity B
KM = 6000 # carrying capacity M
x=np.arange(0,70) # distance from the coast (100m)
y=np.arange(0,90) # distance from the coast
dx=1 # change in x position
dy=1 # change in y position
dt=0.1 # time step
m=71 # number of x steps
o=91 # number of y steps
n=1001 # time
alpha =  0.000002 # predation rate
beta = 0 # growth rate from predation (assumed to be zero)

B=np.zeros(shape=(m,o,n)) # density of species B at (x,y) at time t
M=np.zeros(shape=(m,o,n)) # density of species B at (x,y) at time t
D=0.035 # diffusivity of B
D2=0.005 # diffusivity of M
Alpha=(D*dt)/(dx*dx)
Beta=(D*dt)/(dy*dy) 
Alpha2=(D2*dt)/(dx*dx)
Beta2 = (D2*dt)/(dy*dy)

M[0,:,0]=0 #initial conditions
M[m-1,:,0]=0
M[:,0,0]=0
M[:,o-1,0]=0
B[0,:,0]=0 #initial conditions
B[m-1,:,0]=0
B[:,0,0]=0
B[:,o-1,0]=0
B[1:26,1:o-1,0]=0.96
B[26:44,1:o-1,0]=4.6
B[44:m-1,1:o-1,0]=0.96
B[1:m-1,1:31,0]=0.96
B[1:m-1,59:o-1,0]=0.96
M[1:26,:,0]=2500
M[26:44,:,0]=1000
M[44:m-1,:,0]=2500
M[:,1:31,0]=2500
M[:,59:o-1,0]=2500
for k in range(0,n-1): # loop for time
    B[0,:,k+1]=B[1,:,k+1] # boundary conditions
    B[m-1,:,k+1]=B[m-2,:,k+1]
    M[0,:,k+1]=0
    M[m-1,:,k+1]=0
    B[:,0,k+1]=B[:,1,k+1]
    B[:,o-1,k+1]=B[:,m-2,k+1]
    M[:,0,k+1]=0
    M[:,o-1,k+1]=0
    for i in range(1,m-1): # loop for x
        for j in range(1,o-1): # loop for y
            Bdxx=(-2*Alpha)*B[i,j,k]+Alpha*B[i+1,j,k]+Alpha*B[i-1,j,k]
            Bdyy=(-2*Beta)*B[i,j,k]+Beta*B[i,j+1,k]+Beta*B[i,j-1,k]
            B[i,j,k+1]=B[i,j,k]+Bdxx+Bdyy+r1*B[i,j,k]*(1-B[i,j,k]/KB)-alpha*M[i,j,k]*B[i,j,k] # numerical solution for B 
            Mdxx=(-2*Alpha2)*M[i,j,k]+Alpha2*M[i+1,j,k]+Alpha2*M[i-1,j,k]
            Mdyy=(-2*Beta2)*M[i,j,k]+Beta2*M[i,j+1,k]+Beta2*M[i,j-1,k]
            M[i,j,k+1]=M[i,j,k]+Mdxx+Mdyy+r2*M[i,j,k]*(1-M[i,j,k]/KM)+beta*M[i,j,k]*B[i,j,k] # numerical solution for M
            if B[i,j,k] < 0:
                B[i,j,k] = 0

plt.pcolormesh(B[:,:,n-1]) 
plt.colorbar()
plt.xlabel('X Position')
plt.ylabel('Y Position')

0 个答案:

没有答案