如何自动调整我的散点图标签而不被 python 中的其他标签重叠?

How can I auto-adjust my scatterplot labels without them being overlapped by other labels in python?

所以我一直在研究这个,只是想看看是否有人可以看看为什么我可以自动调整我的散点图标签。在寻找解决方案时,我遇到了在此处 https://github.com/Phlya/adjustText 找到的 adjustText 库,它似乎应该可以工作,但我只是想找到一个从数据帧绘制的示例。当我尝试复制 adjustText 示例时,它抛出一个错误所以这是我当前的代码。

  df["category"] = df["category"].astype(int)
  df2 = df.sort_values(by=['count'], ascending=False).head()
  ax = df.plot.scatter(x="category", y="count")
  a = df2['category']
  b = df2['count']
  texts = []
 for xy in zip(a, b):
        texts.append(plt.text(xy))
    adjust_text(texts, arrowprops=dict(arrowstyle="->", color='r', lw=0.5))

plt.title("Count of {column} in {table}".format(**sql_dict))

但后来我得到了这个 TypeError: TypeError: text() missing 2 required positional arguments: 'y' and 's' 这就是我试图将其转换为枢轴坐标的方法,它有效但坐标只是重叠。

    df["category"] = df["category"].astype(int)
    df2 = df.sort_values(by=['count'], ascending=False).head()
    ax = df.plot.scatter(x="category", y="count")
    a = df2['category']
    b = df2['count']
    for xy in zip(a, b):
        ax.annotate('(%s, %s)' % xy, xy=xy)

正如您在这里看到的,我正在从 sql 中的 table 构建我的 df,我将在此处为您提供这个特定的 table 应该是什么样子。在这个特定的 table 中,它是停留天数与停留那么长时间的人数之比。 因此,作为数据样本可能看起来像。我在上面制作了第二个 datframe,所以我只会在图上标记最高值。这是我第一次在 python 中进行图形可视化体验,因此我们将不胜感激。

[![picture of a graph of overlapping items][1]][1]

[los_days 计数] 3 350 1 4000 15 34

等等。非常感谢。如果您还需要什么,请告诉我。

这里是 df 的一个例子

       category  count
0          2  29603
1          4  33980
2          9  21387
3         11  17661
4         18  10618
5         20   8395
6         27   5293
7         29   4121

在使用 adjustText 库中的示例和我自己的示例进行一些逆向工程之后,我只需要更改我的 for 循环即可创建标签,并且效果非常好。

    labels = ['{}'.format(i) for i in zip(a, b)]
    texts = []
    for x, y, text in zip(a, b, labels):
        texts.append(ax.text(x, y, text))
    adjust_text(texts, force_text=0.05, arrowprops=dict(arrowstyle="-|>",
                                                        color='r', alpha=0.5))