Matplotlib: how to individually mark points?

With maptplotlib, I draw several points using the method scatter(see code below). I would like to mark each point separately.

This code will mark each point with an array labels, but I would like my first item to be marked labels[0], the second one labels[1], etc.

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 this? And BTW, is there any way to iterate over points in ax? The method ax.get_children()gives data that I do not understand.

Thank!

+4
source share
2 answers

Assuming that you are not drawing a lot of scatter points, you can simply do scatterfor each 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)

, , , .

, ax.get_children() , , :

[<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>]

, - ax.collections. list, collections, ( PathCollection).

In [9]: ax.collections
Out[9]: [<matplotlib.collections.PathCollection at 0x10308ba10>]

scatter , , :

# iterate over points and turn them all red
for point in ax.collections:
    point.set_facecolor("red") 
+4

:

# import stuff
import matplotlib.pyplot as plt
import numpy as np

# create dictionary we will close over (twice)
label_dict = dict()
# helper function to do the scatter plot + shove data into label_dict
def lab_scatter(ax, x, y, label_list, *args, **kwargs):
    if 'picker' not in kwargs:
        kwargs['picker'] = 3
    sc = ax.scatter(x, y, *args, **kwargs)
    label_dict[sc] = label_list
    return sc
# call back function which also closes over label_dict, should add more sanity checks
# (that artist is actually in the dict, deal with multiple hits in ind ect)
def cb_fun(event):
    # grab list of labels from the dict, print the right one
    print label_dict[event.artist][event.ind[0]]
# create the figure and axes to use
fig, ax = plt.subplots(1, 1)
# loop over 5 synthetic data sets
for j in range(5):
    # use our helper function to do the plotting
    lab_scatter(ax,
                np.ones(10) * j,
                np.random.rand(10),
                # give each point a unique label
                label_list = ['label_{s}_{f}'.format(s=j, f=k) for k in range(10)])
# connect up the call back function
cid = fig.canvas.mpl_connect('pick_event', cb_fun)
+2

Source: https://habr.com/ru/post/1536826/


All Articles