plt.plot怎么标注最大值和最小值
时间: 2023-10-13 10:13:19 浏览: 512
可以使用 `numpy` 库找到最大值和最小值的索引,然后使用 `annotate` 函数在对应的点上标注最大值和最小值。
下面是一个示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
# 找到最大值和最小值的索引
max_idx = np.argmax(y)
min_idx = np.argmin(y)
# 在最大值和最小值处标注文本
plt.annotate('Maximum', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx]+1, y[max_idx]+0.5),
arrowprops=dict(facecolor='red', shrink=0.05))
plt.annotate('Minimum', xy=(x[min_idx], y[min_idx]), xytext=(x[min_idx]+1, y[min_idx]-0.5),
arrowprops=dict(facecolor='green', shrink=0.05))
plt.show()
```
运行后,会在最大值和最小值的点上标注文本,并带有箭头指示,如下图所示:

阅读全文
相关推荐



















