0

So far, I am able to generate the basic decimal version as seen below: What I managed

The code I used to generate it:

import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(8,4))
x = np.linspace(-20,20,1000)
plt.plot(x,stats.norm.pdf(x,0,5),'r')
plt.plot(x,stats.norm.pdf(x,0,10),'b')
plt.title("Flattening the curve")
plt.savefig("graph.png")
plt.show()

What I want is that x-axis and y-axis begins at 0 and show integers instead of floating values. Something similar to this but as a line graph instead of a histogram:

What I want to generate

Any tips on how to do so? Any help would be much appreciated. Thank you for reading!

2 Answers 2

1

Your mean is at 0 and since normal distribution is symmetric around mean, your graph will go into -ve axis. So move your mean toward positive and start the x values from 0

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

plt.figure(figsize=(8,4))
x = np.linspace(0,40,1000)
plt.plot(x,stats.norm.pdf(x, 20,5),'r')
plt.plot(x,stats.norm.pdf(x,20,10),'b')
plt.title("Flattening the curve")
plt.savefig("graph.png")
plt.show()

enter image description here

The y axis is the pdf values such that

  • Area under curve == 1
  • pdf value is non negative

To get integers you can scale all the values of pdf by some constant factor (say 100), but the graph will lose the purpose if you do not explicitly mention the scaling value used. You should check why you want integers on y axis. To scale y axis you can use

plt.plot(x,stats.norm.pdf(x, 20,5)*100,'r')

1

Use plt.ylim(ymin=0) to let the y start at zero. Set x_mean to some value, e.g. 90 in stats.norm.pdf(x, x_mean, 5) and set x = np.linspace(x_mean-20, x_mean+20, 1000) to have the x values shifted.

The total area of a pdf is 1. To have it similar in height to a histogram, it has to be multiplied by the total number of subjects, and divided by the width of the histogram bins (these bins need to be evenly spread).

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

total_pop = 100000
bin_dist = 1
plt.figure(figsize=(8, 4))
x = np.linspace(0, 70, 500)
plt.plot(x, stats.norm.pdf(x, 25, 5) * total_pop / bin_dist, 'r')
plt.plot(x, stats.norm.pdf(x, 40, 10) * total_pop / bin_dist, 'b')
plt.ylim(ymin=0)
plt.show()

example plot

Note that the curve that is often shown is an exponential growth (logistic curve) that dampens as more individuals get immune. It is not a statistical probability distribution curve, as it shows the number of infected individuals over time. This numberphile video explains more about such curves.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.