简体   繁体   中英

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)

How can I put at least 10 different plots in one figure?

you should always use matplotlib plots as an object, such as:

fig, ax = plt.subplots()

This makes it a lot clearer what you're doing and allows you to plot several lines/scatter/etc on the same axes. Note: This was a bit confusing to me first, but matplotlib calls a 'plot' an axes. So it doesn't have anything to do with the X, Y axis on the plot.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM