Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Addresses issue #24618 "Road sign" boxstyle/annotation, alternative to #24697#24744
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
Abitamim wants to merge5 commits intomatplotlib:mainChoose a base branch fromAbitamim:adjustable-arrow-head-annotation
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+112 −13
Draft
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
e1241a3
Make arrow annotation arrowhead adjustable
Abitamim792c229
Add test - not sure if correct
Abitamim0e890ec
Update tutorial
Abitamimed8396a
Merge branch 'main' of https://github.com/matplotlib/matplotlib into …
Abitamim94907fe
Fixed linting
AbitamimFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
92 changes: 84 additions & 8 deletionslib/matplotlib/patches.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2372,14 +2372,35 @@ def __call__(self, x0, y0, width, height, mutation_size): | ||
class LArrow: | ||
"""A box in the shape of a left-pointing arrow.""" | ||
def __init__(self, pad=0.3, head_width=1.5, head_angle=90.0): | ||
""" | ||
Parameters | ||
---------- | ||
pad : float, default: 0.3 | ||
The amount of padding around the original box. | ||
head_width : float, default: 1.5 | ||
The width of the arrow head versus the body. The minimum value | ||
is 0.0 and the maximum value is 10.0. Any value smaller or | ||
greater than this is contrained to the edge values. | ||
head_angle : float, default: 90.0 | ||
The inside angle of the tip of the arrow. The minimum value is | ||
10.0 and the maximum value is 179.0. Any value smaller or | ||
greater than this is contrained to the edge values. | ||
""" | ||
self.pad = pad | ||
if head_width > 10: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This can be done as | ||
self.head_width = 10 | ||
elif head_width < 0: | ||
self.head_width = 0 | ||
else: | ||
self.head_width = head_width | ||
if head_angle >= 180: | ||
self.head_angle = 179 | ||
elif head_angle < 10: | ||
self.head_angle = 10 | ||
else: | ||
self.head_angle = head_angle | ||
def __call__(self, x0, y0, width, height, mutation_size): | ||
# padding | ||
@@ -2394,10 +2415,26 @@ def __call__(self, x0, y0, width, height, mutation_size): | ||
dxx = dx / 2 | ||
x0 = x0 + pad / 1.4 # adjust by ~sqrt(2) | ||
# The width adjustment is the value that must be added or | ||
# subtracted from y_0 and y_1 for the ends of the head. | ||
# The body width is 2dx. | ||
# Subtracting 1 from the head_width gives what percentage of the | ||
# body width is in the head, and there is .5x of that on each side. | ||
# The .5 cancels out the 2dx for the body width. | ||
width_adjustment = (self.head_width - 1) * dx | ||
# The angle adjustment is the value that must be subtracted/added | ||
# from x_0 or x_1 for the position of the tip. | ||
# each half of the arrow head is a right angle triangle. Therefore, | ||
# each half of the arrow head has the equation tan(head_angle/2)= | ||
# (dx+width_adjustment)/(dxx+angle_adjustment). | ||
angle_adjustment = ((dx + width_adjustment) / math.tan((self. | ||
head_angle/2) * (math.pi/180))) - dxx | ||
return Path._create_closed( | ||
[(x0 + dxx, y0), (x1, y0), (x1, y1), (x0 + dxx, y1), | ||
(x0 + dxx, y1 +width_adjustment), (x0 -angle_adjustment, y0 | ||
+ dx),(x0 + dxx, y0 -width_adjustment), # arrow | ||
(x0 + dxx, y0)]) | ||
@_register_style(_style_list) | ||
@@ -2415,14 +2452,35 @@ class DArrow: | ||
"""A box in the shape of a two-way arrow.""" | ||
# Modified from LArrow to add a right arrow to the bbox. | ||
def __init__(self, pad=0.3, head_width=1.5, head_angle=90.0): | ||
""" | ||
Parameters | ||
---------- | ||
pad : float, default: 0.3 | ||
The amount of padding around the original box. | ||
head_width : float, default: 1.5 | ||
The width of the arrow head versus the body. The minimum value | ||
is 0.0 and the maximum value is 10.0. Any value smaller or | ||
greater than this is contrained to the edge values. | ||
head_angle : float, default: 90.0 | ||
The inside angle of the tip of the arrow. The minimum value is | ||
10.0 and the maximum value is 179.0. Any value smaller or | ||
greater than this is contrained to the edge values. | ||
""" | ||
self.pad = pad | ||
if head_width > 10: | ||
self.head_width = 10 | ||
elif head_width < 0: | ||
self.head_width = 0 | ||
else: | ||
self.head_width = head_width | ||
if head_angle >= 180: | ||
self.head_angle = 179 | ||
elif head_angle < 10: | ||
self.head_angle = 10 | ||
else: | ||
self.head_angle = head_angle | ||
def __call__(self, x0, y0, width, height, mutation_size): | ||
# padding | ||
@@ -2438,13 +2496,31 @@ def __call__(self, x0, y0, width, height, mutation_size): | ||
dxx = dx / 2 | ||
x0 = x0 + pad / 1.4 # adjust by ~sqrt(2) | ||
# The width adjustment is the value that must be added or | ||
# subtracted from y_0 and y_1 for the ends of the head. | ||
# The body width is 2dx. | ||
# Subtracting 1 from the head_width gives what percentage of the | ||
# body width is in the head, and there is .5x of that on each side. | ||
# The .5 cancels out the 2dx for the body width. | ||
width_adjustment = (self.head_width - 1) * dx | ||
# The angle adjustment is the value that must be subtracted/added | ||
# from x_0 or x_1 for the position of the tip. | ||
# each half of the arrow head is a right angle triangle. Therefore, | ||
# each half of the arrow head has the equation tan(head_angle/2)= | ||
# (dx+width_adjustment)/(dxx+angle_adjustment). | ||
angle_adjustment = ((dx + width_adjustment) / math.tan((self. | ||
head_angle/2) * (math.pi/180))) - dxx | ||
return Path._create_closed([ | ||
(x0 + dxx, y0), (x1, y0), # bot-segment | ||
(x1, y0 - width_adjustment), | ||
(x1 + angle_adjustment + dxx, y0 + dx), | ||
(x1, y1 + width_adjustment), # right-arrow | ||
(x1, y1), (x0 + dxx, y1), # top-segment | ||
(x0 + dxx, y1 + width_adjustment), | ||
(x0 - angle_adjustment, y0 + dx), | ||
(x0 + dxx, y0 - width_adjustment), # left-arrow | ||
(x0 + dxx, y0)]) | ||
@_register_style(_style_list) | ||
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletionslib/matplotlib/tests/test_arrow_patches.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletionstutorials/text/annotations.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.