python3 的 adjustment_text 移动文本效果很差

问题描述 投票:0回答:1

我的脚本中有重叠的文本:

import matplotlib.pyplot as plt
from adjustText import adjust_text
x = [12,471,336,1300]
y = [2,5,4,11]
z = [0.1,0.2,0.3,0.4]
im = plt.scatter(x, y, c = z, cmap = "gist_rainbow", alpha = 0.5)
plt.colorbar(im)
texts = []
texts.append(plt.text(783, 7.62372448979592, 'TRL1'))
texts.append(plt.text(601, 6.05813953488372, 'CFT1'))
texts.append(plt.text(631, 4.28164556962025, 'PTR3'))
texts.append(plt.text(665, 7.68018018018018, 'STT4'))
texts.append(plt.text(607, 5.45888157894737, 'RSC9'))
texts.append(plt.text(914, 4.23497267759563, 'DOP1'))
texts.append(plt.text(612, 7.55138662316476, 'SEC8'))
texts.append(plt.text(766, 4.1264667535854, 'ATG1'))
texts.append(plt.text(681, 3.80205278592375, 'TFC3'))
plt.show()

显示重叠文本:

但是,当我添加

adjust_text
时:

import matplotlib.pyplot as plt
from adjustText import adjust_text
x = [12,471,336,1300]
y = [2,5,4,11]
z = [0.1,0.2,0.3,0.4]
im = plt.scatter(x, y, c = z, cmap = "gist_rainbow", alpha = 0.5)
plt.colorbar(im)
data = [
    (783, 7.62372448979592, 'TRL1'),
    (601, 6.05813953488372, 'CFT1'),
    (631, 4.28164556962025, 'PTR3'),
    (665, 7.68018018018018, 'STT4'),
    (607, 5.45888157894737, 'RSC9'),
    (914, 4.23497267759563, 'DOP1'),
    (612, 7.55138662316476, 'SEC8'),
    (766, 4.1264667535854, 'ATG1'),
    (681, 3.80205278592375, 'TFC3')
]

texts = [plt.text(x, y, l) for x, y, l in data]
adjust_text(texts)
plt.savefig('adjust.text.png', bbox_inches='tight', pad_inches = 0.1)

标签被移至左下角,使它们变得无用,而不是稍微重叠。

我正在遵循以下两个链接所建议的

adjust_text(texts)
的线索,

如何调整Matplotlib散点图中的文本以使散点不重叠?

https://adjusttext.readthedocs.io/en/latest/Examples.html

我明白了:

如何让

adjust_text
修复重叠标签?

python python-3.x matplotlib
1个回答
1
投票

你没有显示你如何调用

adjust_text
,但像下面这样做,对我有用:

# top of the code..

adjust_text(texts)

plt.show()

顺便说一句,您还可以简化制作

texts
列表的方式:

data = [
    (783, 7.62372448979592, 'TRL1'),
    (601, 6.05813953488372, 'CFT1'),
    (631, 4.28164556962025, 'PTR3'),
    (665, 7.68018018018018, 'STT4'),
    (607, 5.45888157894737, 'RSC9'),
    (914, 4.23497267759563, 'DOP1'),
    (612, 7.55138662316476, 'SEC8'),
    (766, 4.1264667535854, 'ATG1'),
    (681, 3.80205278592375, 'TFC3')
]

texts = [plt.text(x, y, l) for x, y, l in data]

© www.soinside.com 2019 - 2024. All rights reserved.