0

I have created a simple pyplot of three Gaussians. Now, I want to draw a dotted straight line from the x-axis to the peak value of each Gaussian. Is this possible?

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

mu1 = 5
mu2 = 10
variance1 = 3
variance2 = 1
sigma1 = math.sqrt(variance1)
sigma2 = math.sqrt(variance2)

mu_combined = (variance2*mu1 + variance1*mu2)/(variance1 + variance2)
variance_combined = 1/(1/variance1 + 1/variance2)
sigma_combined = math.sqrt(variance_combined)

x = np.linspace(0,15,100)
plt.plot(x,norm.pdf(x, mu1, sigma1),'b')
plt.plot(x,norm.pdf(x, mu2, sigma2),'r')
plt.plot(x,norm.pdf(x, mu_combined, sigma_combined),'g')
plt.plot([5,5],[0,0.23],'b:')
plt.plot([10,10],[0,0.4],'r:')
plt.plot([8.7,8.7],[0,0.46],'g:')

ax = plt.gca()
ax.set_xlabel('Random Variable')
ax.set_ylabel('Probability')
plt.grid()
plt.show()

plt.savefig('Program_Path/Gaussians.svg', format='svg')
Luk
  • 1,009
  • 2
  • 15
  • 33
  • You might want to look at this ( unfortunately not that easy) answer https://stackoverflow.com/a/49194458/803359. Will even all you to zoom in and out, you have to modify it though. If you do not care about that. other answers there might help as well. – mikuszefski Mar 08 '19 at 13:41
  • Most simple solution `plt.plot( [ x0, x0 ], [ 0, y0 ] )`, where `( x0, y0 )` is the position of your max. – mikuszefski Mar 08 '19 at 13:43
  • @mikuszefski: Thx a lot, man! I have included your answer to my code and it looks fine. Now I am trying to save the figure as svg. It seems to work, i.e. I get a file.svg image in my specified directory. But I cannot open it, or rather when I open the file it is empty. I know this is a different question, but do you have an idea what could be the problem? – Luk Mar 08 '19 at 14:52
  • Yep....known thing...look here: https://stackoverflow.com/a/21884187/803359 – mikuszefski Mar 08 '19 at 15:40

0 Answers0