开发者

How to put individual tags for a matplotlib scatter plot?

开发者 https://www.devze.com 2023-02-14 03:47 出处:网络
I am trying开发者_开发问答 to do a scatter plot in matplotlib and I couldn\'t find a way to add tags to the points. For example:

I am trying开发者_开发问答 to do a scatter plot in matplotlib and I couldn't find a way to add tags to the points. For example:

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

I want for the points in "y" to have labels as "point 1", "point 2", etc. I couldn't figure it out.


Perhaps use 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()

How to put individual tags for a matplotlib scatter plot?


Another option could be using plt.text. Here is a reproducible example:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(20)

N = 5
X=np.random.randint(10, size=(N))
Y=np.random.randint(10, size=(N))

data = np.random.random((N, 4))
annotations= ['point {0}'.format(i) for i in range(N)]

plt.figure(figsize=(8,6))
plt.scatter(X, Y, s=100, color = "blue")
plt.xlabel("X")
plt.ylabel("Y")
for i, label in enumerate(annotations):
    plt.text(X[i]-0.6, Y[i]+0.3,label, rotation = -45,
            bbox=dict(boxstyle="rarrow,pad=0.3", alpha = 0.3))

plt.show()

Output:

How to put individual tags for a matplotlib scatter plot?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号