The original question has been edited, this answer has been updated to reflect this.
When trying to work with LaTeX formatting in matplotlib you must use raw strings, denoted by r""
.
The code given below will iterate over range(4)
and plot using i'th
mean and variance (as you originally have done). It will also set the label
for each plot using label=r'$v_{}$'.format(i+1)
. This string formatting simply replaces the {}
with whatever is called inside format
, in this case i+1
. In this way you can automate the labels for your plots.
I have removed the plt.axvline(...)
, plt.xlabel(...)
and plt.ylabel(...)
out of the for
loop as you only need to call it once. I've also removed the plt.legend()
from the for
loop for the same reason and have removed its arguments. If you supply the keyword argument label
to plt.plot()
then you can label your plots individually as you plot them.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
mean = [10,12,16,22,25]
variance = [3,6,8,10,12]
x = np.linspace(0,40,1000)
for i in range(4):
sigma = np.sqrt(variance[i])
y = mlab.normpdf(x,mean[i],sigma)
plt.plot(x,y, label=r'$v_{}$'.format(i+1))
plt.xlabel("X")
plt.ylabel("P(X)")
plt.axvline(x=15, ymin=0, ymax=1,ls='--',c='black')
plt.legend()
plt.show()