[英]Plot multiple random graph in one plot python
I want to plot multiple plot in one graph, each plot is a random plot of stoichatic python code here;
import numpy as np
import matplotlib.pyplot as plt
N=20
x=[10]
b=0.02
d=0.03
for i in range(0,1000):
if x[i]<N:
birth_prob=b*x[i]
else:
birth_prob=0
death_prob=d*x[i]
random=np.random.uniform(0,1)
if random<birth_prob:
xi=x[i]+1
elif random<birth_prob+death_prob:
xi=x[i]-1
else:
xi=x[i]
x.append(xi)
plt.plot(x)
如何将至少 10 个不同的图放在一个图中?
您应该始终使用 matplotlib 图作为 object,例如:
fig, ax = plt.subplots()
这使您在做什么更清楚,并允许您在同一轴上 plot 几条线/分散/等。 注意:这首先让我有点困惑,但 matplotlib 将“绘图”称为轴。 所以它与 plot 上的 X、Y 轴没有任何关系。
import numpy as np
import matplotlib.pyplot as plt
rows = 5
cols = 2
fig, ax = plt.subplots(rows, cols, figsize=(7,14))
for row in range(0,rows):
for col in range(0,cols):
N=20
x=[10]
b=0.02
d=0.03
for i in range(0,1000):
if x[i]<N:
birth_prob=b*x[i]
else:
birth_prob=0
death_prob=d*x[i]
random=np.random.uniform(0,1)
if random<birth_prob:
xi=x[i]+1
elif random<birth_prob+death_prob:
xi=x[i]-1
else:
xi=x[i]
x.append(xi)
ax[row][col].plot(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.