To draw multiple normal distributions at once with Python matplotlib

Asked 2 years ago, Updated 2 years ago, 69 views

We drew a graph by creating the following functional expression with Python.

import numpy as np

import matplotlib.pyplot as plt

x = np.arange(0, 10.1, 0.2)

a = np.zeros(100*51)

def f(x):

  y = np.exp(-0.5*x)*np.sin(x)+np.random.normal(1,0.005,51)

  return y

print(f(x))
# # ->[0.99605648 1.16898336, ..., 0.9965857  0.99681586]

line = plt.plot(x,f(x))

plt.setp(line, color='r', linewidth=0.2)

plt.show()

I made a graph with noise by putting a normal distribution in the existing function, and I want to make 100 of these. For example, is there a way to draw a graph of each variance, leaving the mean as it is and just changing the variance to 0.0005 - 0.0015, and eventually get 100 pictures superimposed on that one sheet?

I think we can use the for statement, but I'm not sure.

python matplotlib plot

2022-09-20 17:44

1 Answers

import numpy as np

import matplotlib.pyplot as plt

x = np.arange(0, 10.1, 0.2)

a = np.zeros(100 * 51)


def f(x, s):
    y = np.exp(-0.5 * x) * np.sin(x) + np.random.normal(1, s, 51)
    return y


for i in range(5, 106):
    s = i * 0.0001
    plt.grid(True)
    line = plt.plot(x, f(x, s), linewidth=0.2)
    print(line)

# # plt.setp(line, color='r', linewidth=0.2)

plt.show()

Add one variance factor to the f function.

It's hard to recognize if I draw all 100.


2022-09-20 17:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.