如何修复这行

huangapple go评论92阅读模式
英文:

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=&#39;blue&#39;, alpha=0.75)

 

import matplotlib.mlab as mlab

y = mlab.normpdf( bins, mu, sigma)

l = plt.plot(bins, y, &#39;r--&#39;, linewidth=5)

plt.xticks(fontsize=12, rotation=0)

plt.yticks(fontsize=12, rotation=90)

plt.xlabel(&#39;x&#39;, fontsize=16, color=&#39;blue&#39;)

plt.ylabel(&#39;normal&#39;, fontsize=16, color=&#39;blue&#39;)

plt.title(r&#39;$\mathrm{Histogram\ of\ IQ:}\ \mu=0.0, \ \sigma=2.0$&#39;)
NameError
Traceback (most recent call last)
  &lt;command-1361783690544063&gt; in &lt;cell line: 1&gt;()
    ----&gt; 1 plt.title(r&#39;$\mathrm{Histogram\ of\ IQ:}\ \mu=0.0, \ \sigma=2.0$&#39;)
NameError: name &#39;plt&#39; 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=&quot;blue&quot;, alpha=0.75)

y = norm.pdf(bins, mu, sigma)

plt.plot(bins, y, &quot;r--&quot;, linewidth=5)

plt.xticks(fontsize=12, rotation=0)
plt.yticks(fontsize=12, rotation=90)

plt.xlabel(&quot;x&quot;, fontsize=16, color=&quot;blue&quot;)
plt.ylabel(&quot;normal&quot;, fontsize=16, color=&quot;blue&quot;)

plt.title(r&quot;$\mathrm{Histogram\ of\ IQ:}\ \mu=0.0, \ \sigma=2.0$&quot;)

plt.show()

Which yields this
如何修复这行

Lastly, I hope this is not the real IQ distribution :')

huangapple
  • 本文由 发表于 2023年8月11日 01:55:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定