使用 matplotlib 库中的 scatter 函数绘制散点图怎么单独设置一个标签
时间: 2024-03-15 17:44:50 浏览: 124
在使用 `matplotlib` 库中的 `scatter` 函数绘制散点图时,你可以使用 `annotate` 函数添加标签。具体步骤如下:
1. 使用 `scatter` 函数绘制散点图,并记录需要标记的点的坐标。
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.scatter(x, y)
```
2. 使用 `annotate` 函数添加标签。其中,`xy` 参数指定标签的位置,`xytext` 参数指定文本的位置,`textcoords` 参数指定文本位置的坐标系,`arrowprops` 参数指定箭头的样式。
```python
# 添加标签
plt.annotate('Point 1', xy=(1, 2), xytext=(1.5, 3),
textcoords='data', arrowprops=dict(arrowstyle='->'))
```
这样,就可以在散点图中的某个点上添加一个标签了。
完整代码示例:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.scatter(x, y)
# 添加标签
plt.annotate('Point 1', xy=(1, 2), xytext=(1.5, 3),
textcoords='data', arrowprops=dict(arrowstyle='->'))
plt.show()
```
阅读全文
相关推荐

















