awad_text:将标签距离设置为线路

发布于 2025-02-07 07:03:20 字数 1299 浏览 0 评论 0原文

我有以下数据框:

d = {'a': [2, 3, 4.5], 'b': [3, 2, 5]}
df = pd.DataFrame(data=d, index=["val1", "val2","val3"])
df.head()
        a  b
val1  2.0  3
val2  3.0  2
val3  4.5  5

我用以下代码绘制了此数据框:

fig, ax=plt.subplots(figsize=(10,10))


ax.scatter(df["a"], df["b"],s=1)



x1=[0, 2512]
y1=[0, 2512]


ax.plot(x1,y1, 'r-')


#set limits:
ax = plt.gca()
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])


#add labels:

TEXTS = []
for idx, names in enumerate(df.index.values):
    x, y = df["a"].iloc[idx], df["b"].iloc[idx]
    TEXTS.append(ax.text(x, y, names, fontsize=12));

# Adjust text position and add lines 
adjust_text(
    TEXTS, 
    expand_points=(2.5, 2.5),
    expand_text=(2.5,2),
    autoalign="xy", 
    arrowprops=dict(arrowstyle="-", lw=1),
    ax=ax
);

“在此处输入图像说明”

但是,我找不到将标签从红对角线推开的方法,以便获得此结果:

< a href =“ https://i.sstatic.net/d6xue.png” rel =“ nofollow noreferrer”>

I have the following dataframe:

d = {'a': [2, 3, 4.5], 'b': [3, 2, 5]}
df = pd.DataFrame(data=d, index=["val1", "val2","val3"])
df.head()
        a  b
val1  2.0  3
val2  3.0  2
val3  4.5  5

I plotted this dataframe with the following code:

fig, ax=plt.subplots(figsize=(10,10))


ax.scatter(df["a"], df["b"],s=1)



x1=[0, 2512]
y1=[0, 2512]


ax.plot(x1,y1, 'r-')


#set limits:
ax = plt.gca()
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])


#add labels:

TEXTS = []
for idx, names in enumerate(df.index.values):
    x, y = df["a"].iloc[idx], df["b"].iloc[idx]
    TEXTS.append(ax.text(x, y, names, fontsize=12));

# Adjust text position and add lines 
adjust_text(
    TEXTS, 
    expand_points=(2.5, 2.5),
    expand_text=(2.5,2),
    autoalign="xy", 
    arrowprops=dict(arrowstyle="-", lw=1),
    ax=ax
);

enter image description here

However, I can not find a way to push the labels away from the red diagonal line, in order to get this result:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2025-02-14 07:03:20

您可以使用常规matplotlib antotate 函数并根据数据点相对于红线的位置来更改偏移的方向:

ax = df.plot.scatter('a', 'b')
ax.set_aspect(1)
ax.plot((0,10), (0,10), 'r-')

offset = np.array([-1, 1])
for s, xy in df.iterrows():
    xy = xy.to_numpy()
    direction = 1 if xy[1] > xy[0] else -1
    ax.annotate(s, xy, xy + direction * offset, ha='center', va='center', arrowprops=dict(arrowstyle='-', lw=1))

You can use the regular matplotlib annotate function and change the direction of the offset depending on the position of the data point relative to the red line:

ax = df.plot.scatter('a', 'b')
ax.set_aspect(1)
ax.plot((0,10), (0,10), 'r-')

offset = np.array([-1, 1])
for s, xy in df.iterrows():
    xy = xy.to_numpy()
    direction = 1 if xy[1] > xy[0] else -1
    ax.annotate(s, xy, xy + direction * offset, ha='center', va='center', arrowprops=dict(arrowstyle='-', lw=1))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文