1

I am trying to plot a normal distribution curve in Python using matplotlib. I followed the accepted answer in the post python pylab plot normal distribution in order to generate the graph.

I would like to know if there is a way of projecting the mu - 3*sigma, mu + 3*sigma and the mean values on both the x-axis and y-axis.

Thanks

EDIT 1 Image for explaining projection example_image.

In the image, I am trying to project the mean value on x and y-axis. I would like to know if there is a way I can achieve this along with obtaining the values (the blue circles on x and y-axis) on x and y-axis.

  • if you have the positions of the point you want to plot, you can do `ax.plot([x0, x0, 0], [0, y0, y0], 'k-')` for each projected point – Paul H Jan 07 '19 at 20:16
  • What do you mean by projecting? Can you give an image example? – jdhao Jan 08 '19 at 04:01
  • Hey @jdhao, I have edited the post and have added an example of what I mean. –  Jan 08 '19 at 10:19

1 Answers1

2

The following script shows how to achieve what you want:

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

mu = 2
variance = 9
sigma = np.sqrt(variance)

x = np.linspace(mu - 3*sigma, mu + 3*sigma, 500)
y = stats.norm.pdf(x, mu, sigma)

fig, ax = plt.subplots()
ax.plot(x, y)

ax.set_xlim([min(x), max(x)])
ax.set_ylim([min(y), max(y)+0.02])

ax.hlines(y=max(y), xmin=min(x), xmax=mu, color='r')
ax.vlines(x=mu, ymin=min(y), ymax=max(y), color='r')

plt.show()

The produced plot is

enter image description here

If you are familiar with the properties of normal distribution, it is easy to know intersection with x axis is just mu, i.e., the distribution mean. Intersection with y axis is just the maximum value of y, i.e, max(y) in the code.

jdhao
  • 24,001
  • 18
  • 134
  • 273