簡體   English   中英

Plot 多個隨機圖在一個 plot python

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM