Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overlap with figure boarder #13

Closed
Surveyor4lyfe opened this issue Mar 8, 2024 · 7 comments
Closed

Overlap with figure boarder #13

Surveyor4lyfe opened this issue Mar 8, 2024 · 7 comments

Comments

@Surveyor4lyfe
Copy link

When running textalloc with set x/y limits for a figure, the text will occasionally overlap with the boarder.

image

You can have the same thing happen when running the below example:

import matplotlib.pyplot as plt
import textalloc as ta

fig, ax = plt.subplots()

labelsE = [0,2]
labelsN= [0,2]
labels=['Sample Text 1','Sample Text 2']

#Set X limit
ax.set_xlim(0,2)

#Set Y limit
ax.set_ylim(0,2)

ta.allocate_text(fig,ax,labelsE,labelsN,
labels,
scatter_plot=ax.scatter(labelsE,labelsN, c='b',s=0),
max_distance=.6,
min_distance=.03,
margin=0.1,
linewidth=0.5,
textsize=6,
draw_all=True,
nbr_candidates=200,
avoid_label_lines_overlap=True)

image

Would it be possible to add an option to state a minimum offset from the plot board?

@ckjellson
Copy link
Owner

I am not able to reproduce an overlap with the border, do you have a way to get such a result?

Regarding the margin between the text and the border I should probably add an option like that, but I would first like to understand how you got the overlapping with the border before fixing this.

Also, I would not recommend setting margin>min_distance in most cases, so you could try changing that, and I will also add a comment about that in the docs.

@Surveyor4lyfe
Copy link
Author

Surveyor4lyfe commented Mar 12, 2024

Thankyou for replying. Try this test code.

import matplotlib.pyplot as plt
import textalloc as ta

dE=[0.0,-0.0016000000000531145,-0.0004000000000132786,-0.005400000000008731,-0.003500000000030923,-0.003199999999992542,-0.006399999999985084,-0.004500000000007276,-0.007100000000036744,-0.009500000000014552,-0.006899999999973261,-0.010499999999979082,-0.011899999999968713,-0.011399999999980537,-0.012950000000058753,-0.015300000000024738,-0.018699999999967076,-0.02030000000002019]
dN=[0.0,0.0003000000000383807,0.0009500000000014552,0.0006000000000767614,-0.0004000000000132786,-0.0004000000000132786,-0.0007999999999128704,-0.0013999999999896318,-0.0029999999999290863,-0.0036999999999807187,-0.002200000000016189,-0.002599999999915781,-0.003399999999942338,-0.003499999999917236,-0.004599999999982174,-0.0050999999999703505,-0.0061999999999216016,-0.007499999999936335]

date=['15/11/1990','13/09/1993','21/03/1995','25/08/1997','22/02/1999','8/05/2001','3/06/2002','1/04/2003','18/04/2005','4/09/2006','18/11/2008','10/03/2010','15/08/2011','9/11/2012','26/08/2014','2/05/2017','23/03/2021','18/12/2023']

#Select Intial,Previous and Current to label
calloutE_labels=[dE[0],dE[-1:][0],dE[-2:-1][0]]
calloutN_labels=[dN[0],dN[-1:][0],dN[-2:-1][0]]
calloutDate_labels=[date[0],date[-1:][0],date[-2:-1][0]]

fig, ax = plt.subplots()

#Set X limit
ax.set_xlim(-0.025,0.005)

#Set Y limit
ax.set_ylim(-0.025,0.005)

#Set aspect ratio to being sqaure
ax.set_aspect('equal')

#Rotate X ticks text by 45 degrees
plt.xticks(rotation=45)

ta.allocate_text(fig,ax,calloutE_labels,calloutN_labels,
calloutDate_labels,
scatter_plot=ax.scatter(dE,dN, c='b',s=5),
max_distance=.6,
min_distance=.05,
margin=0.03,
linewidth=0.5,
textsize=6,
draw_all=True,
nbr_candidates=200,
avoid_label_lines_overlap=True)

It appears to fail if I set the aspect ratio to square. If I remove that one line it works.

With the aspect locked:
image

with the aspect line commented out:
image

I need the aspect ratio locked to square, as I want the the spacing between the tick marks on the X and Y axis to be the same.

@ckjellson
Copy link
Owner

ckjellson commented Mar 17, 2024

Thank you for the example! As you say there seems to be something strange happening when applying aspect equal, and I am not able to pinpoint what it is right away. I will look further into it as soon as possible.

Meanwhile, you could try initializing the figure with a size matching your aspect ratio, for example:

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

I know this is not a good solution, so as mentioned I will look into it as soon as I can!

@Surveyor4lyfe
Copy link
Author

Thankyou for your suggestion!! That has helped me produce the plots I needed to for the mean time! :)

@ckjellson
Copy link
Owner

ckjellson commented Apr 6, 2024

Hi again, I have made an update which no longer should require you to set the figsize to match the aspect ratio. Please let me know if you try it and it does not work as expected.

@ckjellson
Copy link
Owner

This was clearly not solved by what I believed to be a fix, seem to be missing something about what happens when setting the aspect_ratio manually or plotting on an image, reopening until I have worked it out.

@ckjellson
Copy link
Owner

Okay, now I believe this issue is finally solved, even though the aspect had been set the fig does not update automatically, meaning that it had to be redrawn without rendering to update. Now hopefully everything should work properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants