Phlya / adjustText

A small library for automatically adjustment of text position in matplotlib plots to minimize overlaps.
https://adjusttext.readthedocs.io/
MIT License
1.5k stars 87 forks source link

Faraway Text affecting positioning of other Text objects #151

Closed miguelangel-reyes closed 1 year ago

miguelangel-reyes commented 1 year ago

The positioning of Text is correct up until I add the last element, which seems to somehow affect a faraway text object. I have tried playing with the various settings, explode_radius, force_pull, force_text,force_static, with no major success. Force_text is only one that seems to affects how greatly it is affected but cannot be used to fix/position the text label correctly.

This is how it looks before adding the last element. Take note that Text 'Brother' is correctly positioned Screen Shot 2023-06-04 at 10 36 01 AM

However, as soon as I add the last element, Text 'Brother' is affected greatly, even though it is not close to the added text element. Screen Shot 2023-06-04 at 10 35 18 AM

Is there a way to resolve this. I figured a combination of force_text and explode_radius could help, but it doesn't seem like it does. Also the two elements seem far away from each other which makes me wonder why they are affecting eachother and no the other elements that are even closer.

Currently this is how I call adjust_text. Take note that explode_radius does not have any affect. I did not originally have it specified but no values be it default, 50, 10 or 0 affects the positioning of the Text Objects. The same for time_lim, I tried, 0.5, 1, and 1.5.

for X, Y I used linear interpolation as found in the wikis, in order to aid adjust_text in not overlapping with the lines. f = interpolate.interp1d(self.xaxis, self.yaxis) x = np.linspace(min(self.xaxis), max(self.xaxis), 500) y = f(x)

adjust_text(labels, x, y,time_lim=0.5, explode_radius=0,force_pull=(1,1),force_text=(1,1),force_static=(1,1))

UPDATE For anyone that experienced similar, issue was not a collision between the latest Text object added. The root cause seems to be the linear interpolation. After checking the points it was generating, adjust text seems to go a bit crazy, but understandable as the new Text object shifted the interpolation. Increasing the number of points smooths out the linear inteprolation and fixes the positioning. Closing the ticket as it was more of my lack of understanding.