-
Notifications
You must be signed in to change notification settings - Fork 58
Prevent arrow lengths from being too long #2576
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
Conversation
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
tidy3d/components/geometry/base.pyLines 2291-2301 2291 # We only want to set the shape once, so we disconnect ourselves
2292 event.canvas.mpl_disconnect(arrow.set_shape_cb[0])
2293
2294 transform = arrow.axes.transData.transform
! 2295 scale_x = transform((1, 0))[0] - transform((0, 0))[0]
! 2296 scale_y = transform((0, 1))[1] - transform((0, 0))[1]
! 2297 scale = max(scale_x, scale_y) # <-- Hack: This is a somewhat arbitrary choice.
2298 arrow_length = ARROW_LENGTH * event.canvas.figure.get_dpi() / scale
2299
2300 if bend_radius:
2301 v_norm = (direction[0] ** 2 + direction[1] ** 2) ** 0.5 |
|
I updated the equation for
This is probably not the optimal choice either. But at least it guarantees that arrows remain within the plot window.
|
IMPORTANT: This PR does not preserve arrow lengths on screen when the aspect ratio is changed.Do we want that? If you want that, you must use a different approach....That approach is explained here. I tested that google-gemini code and actually works. I did not implement it, because I could not figure out how to make it work with the custom Currently, this PR is a quick hack which makes the minimal code changes to make sure that the arrows are not too large to fit in the plot window. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
1 file reviewed, no comments
Edit PR Review Bot Settings | Greptile
Thanks @jewettaijfc, this looks good to me overall, but I'll feel safer if we check to make sure the notebooks that use this in plotting still look ok (if you haven't already). |
To be clear: no need to check every notebook but maybe just look at around 5 and re-run (even without transpose) just to make sure nothing looks weird. Thanks! |
Please squash commits into one (using and also please add a line under "fixed" in the |
I've checked 25 notebooks that use it this code today. I tested them while I was testing PR2544. Not all of these notebooks have examples that display arrows. But the examples that do have arrows look okay (in my opinion). Here's the only weird side effect of this PR that I've found so far in my tests: Sometimes arrows that were originally finite length become very short (as a result of this PR) when the aspect ratio gets very large. This doesn't happen very often, but it does occasionally happen. The alternative solution I mentioned earlier tries to solve that that issue by preserving the (visual) length of each arrow. But I don't know if it's worth the effort. I personally don't mind seeing short arrows (especially when the aspect ratio is 100). |
03a973f
to
c13a5a7
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops forgot to hit submit!
…ect ratio (`ax.set_aspect()`). This fixes issue #2575.
c13a5a7
to
1f110b9
Compare
This PR is an attempt to address issue #2575.
After this PR, arrows will always fit within plot window, regardless of their direction and regardless of the aspect ratio of the plot (
ax.set_aspect()
).Screenshots
Before this PR
Before this PR, the vertical arrows could easily grow too long to fit within the plot window:

After this PR
After this PR, the green arrow is visible, regardless of it's orientation and regardless of the aspect ratio:

Test files
The plot above was made using this file:
test_arrow_autoscale.ipynb.zip
Download it, unzip it, and run the cells.
(NOTE: I disabled the plot on the right because it depends on #2544. That PR might not be merged by the time you are reading this. But it's not necessary to show this plot to see the problem. It just makes it more obvious.)
Notes:
hlim=[-20,20]
to the 2nd plot() argument list. The short grey arrow will resemble a small sideways triangle.) If you want to preserve the length of short arrows as well, a different approach is needed. (I did not try this because it is more complicated. See below.)Additional context
This problem is making it hard to implement PR2544.
Greptile Summary
Fixed visualization issue in
tidy3d/components/geometry/base.py
where arrows could extend beyond plot boundaries by modifying_arrow_shape_cb
to consider both horizontal and vertical scales when computing arrow lengths.