[英]Matplotlib : how to label points individually?
With maptplotlib, I plot some points with the scatter
method (see code below). 使用maptplotlib,我使用scatter
方法绘制一些点(参见下面的代码)。 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. 此代码将使用labels
数组标记每个点,但我希望我的第一个点标记为labels[0]
,第二个labels[1]
,依此类推。
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
? 顺便说一句,有没有办法迭代ax
的点? The method ax.get_children()
yields data I don't understand. 方法ax.get_children()
产生我不理解的数据。
Thanks! 谢谢!
Assuming that you aren't plotting many scatter points, you could just do a scatter
for every point: 假设您没有绘制许多散点,您可以为每个点做一个scatter
:
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: 要回答问题的第二部分, 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>]
If you are just looking to get the sets of scatter points in your axes, the easiest way is through ax.collections
. 如果您只想在轴上获取散点图集,最简单的方法是通过ax.collections
。 This is a list
that contains all the collections
instances plotted in the axes (scatter points belong to PathCollection
). 这是一个list
,其中包含在轴中绘制的所有collections
实例(散点属于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: 如果您为每个点绘制了单独的scatter
,则迭代这些点非常简单:
# iterate over points and turn them all red
for point in ax.collections:
point.set_facecolor("red")
All of this can be wrapped up an hidden in a function or a class: 所有这些都可以隐藏在函数或类中:
# 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.