pylab:用颜色和标签(ID,而不是类别)绘制点

pylab: plotting points with colors and labels (IDs, not categories)

我正在尝试用颜色和标签绘制点。这不是经典问题:实际上,通常 python 用户将 "labels" 设置为类别。在这种情况下,我希望颜色代表一个特征,而标签是点本身的标识符。 它遵循一个玩具示例:

x = [-0.01611772,  1.51755901, -0.64869352, -1.80850313, -0.11505037]
y = [ 0.04845168, -0.45576903,  0.62703651, -0.24415787, -0.41307092]

colors = ['b', 'g', 'r', 'b', 'r']
labels = ['Gioele', 'Felix', 'Elpi', 'Roro', 'Cacara']

我想使用散点函数。按照 "quick" 文档:

def scatter(x, y, s=20, c=None, marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs) Inferred type: (x: Any, y: Any, s: int, c: Any, marker: unicode, cmap: Any, norm: Any, vmin: Any, vmax: Any, alpha: Any, linewidths: Any, verts: Any, edgecolors: Any, hold: Any, data: Any, kwargs: dict) -> Any

所以,我的尝试是:

import pylab
pylab.scatter(x, y, c=colors, data=labels)
pylab.show()

但似乎忽略了 data=labels 部分。

此外:假设我们可以绘制标签,有没有办法以 "smart" 的方式绘制它们,即标签不会相互隐藏?我需要类似于 R 函数 ggrepel.

的东西

我认为在这里使用 plt.annotate 是一个选项。举个例子:

import matplotlib.pyplot as plt

x = [-0.01611772,  1.51755901, -0.64869352, -1.80850313, -0.11505037]
y = [ 0.04845168, -0.45576903,  0.62703651, -0.24415787, -0.41307092]
colors = ['b', 'g', 'r', 'b', 'r']
labels = ['Gioele', 'Felix', 'Elpi', 'Roro', 'Cacara']

plt.scatter(x,y,c=colors)
for label,xi,yi in zip(labels,x,y):
    plt.annotate(label,xy=(xi,yi),textcoords='offset points',
    ha='left',va='bottom')

这给出了以下输出:

编辑:我刚刚发现您也询问了重叠标签。 This question seems to have a good solution. There is also apparently a piece of code on github 旨在模拟 ggrepel.