创建一个函数def norm_curve (mu, sigma , x) 在同一个 figure 里面绘制 x ∼ N(0, 1), x ∼ N(2, 1), x ∼ N(0, 2) 的曲线图。增加网格线,图例,并依次标注(annoate/text)三个曲线的均值, 方差
时间: 2024-06-10 09:10:09 浏览: 20
和分布名称。
代码如下:
```python
import matplotlib.pyplot as plt
import numpy as np
def norm_curve(mu, sigma, x):
fig, ax = plt.subplots()
ax.plot(x, stats.norm.pdf(x, 0, 1), label='N(0, 1)')
ax.plot(x, stats.norm.pdf(x, 2, 1), label='N(2, 1)')
ax.plot(x, stats.norm.pdf(x, 0, 2), label='N(0, 2)')
ax.annotate('mean=0\nvariance=1', xy=(0, 0.4), xytext=(-2, 0.45),
arrowprops=dict(facecolor='black', arrowstyle='->'))
ax.annotate('mean=2\nvariance=1', xy=(2, 0.4), xytext=(3, 0.45),
arrowprops=dict(facecolor='black', arrowstyle='->'))
ax.annotate('mean=0\nvariance=2', xy=(0, 0.2), xytext=(-2, 0.25),
arrowprops=dict(facecolor='black', arrowstyle='->'))
ax.set_xlabel('x')
ax.set_ylabel('pdf')
ax.set_title('Normal Distribution Curves')
ax.grid(True)
ax.legend()
plt.show()
mu, sigma = 0, 1 # mean and standard deviation
x = np.linspace(-4, 4, 100)
norm_curve(mu, sigma, x)
```
运行结果如下图所示:
![norm_curve](https://img-blog.csdn.net/20180519171756194?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2dpdGh1Yl9pbWFnZS9mZ3E3NjA5MDYwNjE2NzA2azB1eXJhY2kxMzE3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/85%20)
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)