matplotlib 如何用matplot库将数据拆分成两个图形

e0bqpujr  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(220)

如果有人能帮助我,我将非常感谢。我正在matplotib中创建一个图形,但是我很想将while循环创建的14行拆分为P的x和y值,所以不要使用plt.plot(t,P)它将是plt.plot(t,((P[1])[0])))和plt.plot(t,((P[1])[1])))。如果有人能很快地帮助我,我会很高兴,这应该很容易,但我只是得到错误的数组
`

#Altering Alpha in Tumor Cells vs PACCs
#What is alpha? α = Rate of conversion of cancer cells to PACCs
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from google.colab import files
value = -6
counter = -1
array = []
pac = []
while value <= 0:
  def modelP(x,t):
      P, C = x
      λc = 0.0601 
      K = 2000 
      α = 1 * (10**value) 
      ν = 1 * (10**-6) 
      λp = 0.1 
      γ = 2
  

    #returning odes
      dPdt = ((λp))*P*(1-(C+(γ*P))/K)+ (α*C) 
      dCdt = ((λc)*C)*(1-(C+(γ*P))/K)-(α*C) + (ν***P) 
      return dPdt, dCdt


  
  

    #initial
  C0= 256 
  P0 = 0 
  Pinit = [P0,C0]
    

    #time points 
  t = np.linspace(0,730) 

    #solve odes
  P = odeint(modelP,Pinit,t) 
  plt.plot(t,P)
  value += 1 
  
  
  
  
  #plot results
  

plt.xlabel('Time [days]')
plt.ylabel('Number of PACCs')

plt.show()

`

ekqde3dh

ekqde3dh1#

您可以使用subplots()创建两个子图,然后将单独的线绘制到所需的图中。为此,首先在开始处(while循环之前)添加以下线,以添加子图...

fig, ax = plt.subplots(2,1) ## Plot will 2 rows, 1 column... change if required

然后...在while循环中,替换绘图线...

plt.plot(t,P)

with(注意空格,以便这些行位于while循环内)

if value < -3:  ## I am using value = -3 as the point of split, change as needed
    ax[0].plot(t,P)#, ax=ax[0])  ## Add to first plot
else:
    ax[1].plot(t,P)#,ax=ax[1])   ## Add to second plot

这将给予这样一个情节。

相关问题