给上述图表,增加网格线,图例,并依次标注(annoate/text)三个曲线的均值, 方差等信息
时间: 2024-05-11 13:15:21 浏览: 7
以下是增加网格线、图例,并标注三个曲线的均值、方差等信息后的图表:
```python
import matplotlib.pyplot as plt
import numpy as np
# 生成数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
# 绘制图表
fig, ax = plt.subplots()
ax.plot(x, y1, label='sin(x)')
ax.plot(x, y2, label='cos(x)')
ax.plot(x, y3, label='tan(x)')
# 添加网格线
ax.grid(True)
# 添加图例
ax.legend(loc='upper right')
# 标注信息
ax.annotate(f"Mean: {np.mean(y1):.3f}\nStd: {np.std(y1):.3f}", xy=(8, 0), xytext=(8, 0.5),
arrowprops=dict(facecolor='black', shrink=0.05))
ax.annotate(f"Mean: {np.mean(y2):.3f}\nStd: {np.std(y2):.3f}", xy=(3, 0), xytext=(3, -0.5),
arrowprops=dict(facecolor='black', shrink=0.05))
ax.annotate(f"Mean: {np.mean(y3):.3f}\nStd: {np.std(y3):.3f}", xy=(2.5, 5), xytext=(4, 5),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()
```
![image.png](attachment:image.png)