Python:散点图,每个数据点都有对齐的注释

Python: scatter plot with aligned annotations at each data point

我想生成一个包含数十个点的散点图,这些点可能彼此非常接近。我从问题的答案中尝试了注释的方法:

>> matplotlib scatter plot with different text at each data point

但是您可以看到,当点彼此足够接近时,标签/注释重叠。是否有任何库或方法可以生成这样的带有单独注释的图,这些注释不会相互冲突,也不会与图的边界发生冲突?

import matplotlib.pyplot as plt

z = [0.15, 0.3, 0.45, 0.46, 0.6, 0.75]
y = [2.56422, 3.77284, 3.52623, 3.52623, 3.51468, 3.02199]
n = [58, 651, 393, "393(2)", 203, 123]

fig, ax = plt.subplots()
ax.scatter( z, y )

for i, txt in enumerate( n ):
    ax.annotate( txt, ( z[i] + .01, y[i] + .01 ) )

plt.show()

我写了一个库 adjustText 就是这样做的。 https://github.com/Phlya/adjustText

z = [0.15, 0.3, 0.45, 0.46, 0.6, 0.75]
y = [2.56422, 3.77284, 3.52623, 3.52623, 3.51468, 3.02199]
n = [58, 651, 393, "393(2)", 203, 123]

fig, ax = plt.subplots()
ax.scatter( z, y )
texts = []
for i, txt in enumerate( n ):
    texts.append(ax.text(z[i], y[i], txt))
adjust_text(texts)
plt.show()