Issue
I have overlapping text from my script:
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()
however, when I add 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)
the labels are shifted to the lower left corner, making them useless, instead of just a little overlapped.
I am following clues from adjust_text(texts)
as suggested by the below two links,
How to adjust text in Matplotlib scatter plot so scatter points don't overlap?
and https://adjusttext.readthedocs.io/en/latest/Examples.html
how can I get adjust_text
to fix the overlapping labels?
Solution
You don't show how you call adjust_text
but doing it like below, works for me :
# top of the code..
adjust_text(texts)
plt.show()
And BTW, you could also simplify the way you make the texts
list :
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]
Answered By - Timeless
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.