或许可以使用 plt.annotate:
import numpy as np
import matplotlib.pyplot as plt
N = 10
data = np.random.random((N, 4))
labels = ['point{0}'.format(i) for i in range(N)]
plt.subplots_adjust(bottom = 0.1)
plt.scatter(
data[:, 0], data[:, 1], marker='o', c=data[:, 2], s=data[:, 3] * 1500,
cmap=plt.get_cmap('Spectral'))
for label, x, y in zip(labels, data[:, 0], data[:, 1]):
plt.annotate(
label,
xy=(x, y), xytext=(-20, 20),
textcoords='offset points', ha='right', va='bottom',
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'))
plt.show()
![在此输入图片描述](https://istack.dev59.com/b5uhP.webp)
plt.scatter
来移除圆圈。你可以使用plt.annotate(label, xy = (x, y), xytext = (0, 0), textcoords = 'offset points')
在图像上放置任意文本。注意,xytext = (0, 0)
表示没有偏移量,并且省略arrowprops
会使plt.annotate
不绘制箭头。 - unutbuxy=(x, y),xytext = (x - 20, y + 20)
否则它会将所有标签堆叠在一个点上。 - JBWhitmore'point{0}'.format(i)
更改为'point{0}'.format(i+1)
。或者,您可以更改range
:['point{0}'.format(i) for i in range(N)]
为['point{0}'.format(i) for i in range(1,N+1)]
。 - unutbu