英文:
How can I fix this line
问题
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
mu, sigma = 0, 2
x = mu + sigma * np.random.randn(25000)
n, bins, patches = plt.hist(x, 25, facecolor='blue', alpha=0.75)
import matplotlib.mlab as mlab
y = mlab.normpdf(bins, mu, sigma)
l = plt.plot(bins, y, 'r--', linewidth=5)
plt.xticks(fontsize=12, rotation=0)
plt.yticks(fontsize=12, rotation=90)
plt.xlabel('x', fontsize=16, color='blue')
plt.ylabel('normal', fontsize=16, color='blue')
plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=0.0, \ \sigma=2.0$')
NameError
Traceback (most recent call last)
<command-1361783690544063> in <cell line: 1>()
----> 1 plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=0.0, \ \sigma=2.0$')
NameError: name 'plt' is not defined
英文:
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
mu, sigma = 0,2
x= mu + sigma*np.random.randn(25000)
n, bins, patches = plt.hist(x, 25, facecolor='blue', alpha=0.75)
import matplotlib.mlab as mlab
y = mlab.normpdf( bins, mu, sigma)
l = plt.plot(bins, y, 'r--', linewidth=5)
plt.xticks(fontsize=12, rotation=0)
plt.yticks(fontsize=12, rotation=90)
plt.xlabel('x', fontsize=16, color='blue')
plt.ylabel('normal', fontsize=16, color='blue')
plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=0.0, \ \sigma=2.0$')
NameError
Traceback (most recent call last)
<command-1361783690544063> in <cell line: 1>()
----> 1 plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=0.0, \ \sigma=2.0$')
NameError: name 'plt' is not defined
I tried removing line 16, I tried removing section of the code.
答案1
得分: 1
首先,matplotlib.mlab.normpdf
已经被弃用了很多年(实际上是十多年前),你应该使用 scipy.stats.norm.pdf
。
事实上,你的代码中只有一行成功执行,并且你没有调用 plt.show()
,这表明你可能正在使用一个笔记本,如果是这样的话,你应该确保在代码的其余部分之前调用导入语句。
在修正了 normpdf
的问题后,以下代码对我来说可以正常运行:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
mu, sigma = 0, 2
x = mu + sigma * np.random.randn(25000)
n, bins, patches = plt.hist(x, 25, facecolor="blue", alpha=0.75)
y = norm.pdf(bins, mu, sigma)
plt.plot(bins, y, "r--", linewidth=5)
plt.xticks(fontsize=12, rotation=0)
plt.yticks(fontsize=12, rotation=90)
plt.xlabel("x", fontsize=16, color="blue")
plt.ylabel("normal", fontsize=16, color="blue")
plt.title(r"$\mathrm{Histogram\ of\ IQ:}\ \mu=0.0, \ \sigma=2.0$")
plt.show()
这将产生如下图所示的输出:
最后,我希望这不是真正的智商分布 :')
英文:
First, matplotlib.mlab.normpdf
has been deprecated for years now (more than a decade actually), you should use scipy.stats.norm.pdf
instead
The fact that a single line executes and that you're not calling plt.show() indicates that you may be using a notebook, in which case you should make sure that you call the imports before the remaining of the code
After correcting the normpdf thing the code works for me:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
mu, sigma = 0, 2
x = mu + sigma * np.random.randn(25000)
n, bins, patches = plt.hist(x, 25, facecolor="blue", alpha=0.75)
y = norm.pdf(bins, mu, sigma)
plt.plot(bins, y, "r--", linewidth=5)
plt.xticks(fontsize=12, rotation=0)
plt.yticks(fontsize=12, rotation=90)
plt.xlabel("x", fontsize=16, color="blue")
plt.ylabel("normal", fontsize=16, color="blue")
plt.title(r"$\mathrm{Histogram\ of\ IQ:}\ \mu=0.0, \ \sigma=2.0$")
plt.show()
Lastly, I hope this is not the real IQ distribution :')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论