Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put individual tags for a matplotlib scatter plot?

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.

like image 618
J. Velazquez-Muriel Avatar asked Oct 21 '22 18:10

J. Velazquez-Muriel


People also ask

How do you add labels to a scatter plot?

Do add the data labels to the scatter chart, select the chart, click on the plus icon on the right, and then check the data labels option. This will add the data labels that will show the Y-axis value for each data point in the scatter graph.

How do you label points on a scatter plot in Python?

To label the scatter plot points in Matplotlib, we can use the matplotlib. pyplot. annotate() function, which adds a string at the specified position. Similarly, we can also use matplotlib.


1 Answers

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

enter image description here

like image 383
unutbu Avatar answered Oct 23 '22 06:10

unutbu