如何为散点图放置单个标记

x-A*_*A-x 212 python matplotlib

我试图在matplotlib中做一个散点图,我找不到向点添加标签的方法.例如:

scatter1=plt.scatter(data1["x"], data1["y"], marker="o",
                     c="blue",
                     facecolors="white",
                     edgecolors="blue")
Run Code Online (Sandbox Code Playgroud)

我希望"y"中的点标记为"点1","点2"等.我无法弄明白.

unu*_*tbu 368

也许使用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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @Vladtn:您可以通过省略`plt.scatter`来删除圆圈.您可以使用`plt.annotate(label,xy =(x,y),xytext =(0,0),textcoords ='offset points')`在图像上放置任意文本.注意`xytext =(0,0)`表示没有偏移,省略`arrowprops`导致`plt.annotate`不绘制箭头. (16认同)
  • 这就是我要说的.链接到文档:http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.annotate演示链接:http://matplotlib.sourceforge.net/examples/pylab_examples/annotation_demo2.html (7认同)
  • @ubuntu是否可以使用数字(或标签)_instead_点? (4认同)