python画图代码,并标出指定值
时间: 2024-02-05 13:02:55 浏览: 19
以下是一个用Matplotlib库绘制带标注点的折线图的Python代码,你可以根据需要修改数据、标注点的位置和样式等参数:
```python
import matplotlib.pyplot as plt
# 数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
annotation_x = 3
annotation_y = 6
# 创建画布和子图
fig, ax = plt.subplots()
# 绘制折线图
ax.plot(x, y)
# 添加标注点
ax.scatter(annotation_x, annotation_y, color='red')
ax.annotate('(%s, %s)' % (annotation_x, annotation_y), xy=(annotation_x, annotation_y), xytext=(annotation_x + 0.5, annotation_y + 1), arrowprops=dict(facecolor='red', arrowstyle='->'))
# 设置标题和轴标签
ax.set_title('Line Chart with Annotation Point')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
# 显示图形
plt.show()
```
在代码中,我们使用了 `scatter` 函数添加了一个标注点,并使用 `annotate` 函数添加了标注文本和箭头。你可以根据需要修改标注点的位置、颜色和标注文本的样式等参数。