我的脚本中有重叠的文本:
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
修复重叠的标签?
您没有显示您如何调用
adjust_text
,但像下面这样对我有用:顺便说一句,您还可以简化列出列表的方式
texts
: