Matplotlib : how to label points individually?
label, matplotlib, python, scatter
Solution
Assuming that you aren't plotting many scatter points, you could just do a `scatter` for every point:
import numpy as np; import matplotlib.pyplot as plt
y = np.arange(10) # points to plot
x=np.arange(10)
labels = np.arange(10) # labels of the points
fig, ax = plt.subplots(nrows=1, ncols=1)
for x_,y_,label in zip(x,y,labels):
ax.scatter([x_], [y_], label=label, picker=3)
This will start to lag if you are plotting many thousands or tens of thousands of points, but if it is just a few, then it is no problem.
To answer the second part of your question, `ax.get_children()` returns a list of objects that compose those axes, for instance:
[<matplotlib.axis.XAxis at 0x103acc410>,
<matplotlib.axis.YAxis at 0x103acddd0>,
<matplotlib.collections.PathCollection at 0x10308ba10>, #<--- this is a set of scatter points
<matplotlib.text.Text at 0x103082d50>,
<matplotlib.patches.Rectangle at 0x103082dd0>,
<matplotlib.spines.Spine at 0x103acc2d0>,
<matplotlib.spines.Spine at 0x103ac9f90>,
<matplotlib.spines.Spine at 0x103acc150>,
<matplotlib.spines.Spine at 0x103ac9dd0>]
If you are just looking to get the sets of scatter points in your axes, the easiest way is through `ax.collections`. This is a `list` that contains all the `collections` instances plotted in the axes (scatter points belong to `PathCollection`).
In [9]: ax.collections
Out[9]: [<matplotlib.collections.PathCollection at 0x10308ba10>]
If you have plotted a separate `scatter` for each point, iterating over the points is as easy as:
# iterate over points and turn them all red
for point in ax.collections:
point.set_facecolor("red")
Problem
With maptplotlib, I plot some points with the `scatter` method (see code below). I would like to label each point individually. This code will label every point with the `labels` array, but I would like my first point to be labeled with `labels[0]`, the second with `labels[1]` and so on. ``` import numpy as np; import matplotlib.pyplot as plt y = np.arange(10) # points to plot labels = np.arange(10) # labels of the points fig, ax = plt.subplots(nrows=1, ncols=1) ax.scatter(x=np.arange(10), y=y, label=labels, picker=3) ``` Is there any way to do that? And BTW, is there any way to iterate through the points in `ax`? The method `ax.get_children()` yields data I don't understand. Thanks!