Add add_abline for drawing a line from a slope and an intercept - #5668
Open
kirthi-b wants to merge 1 commit into
Open
Add add_abline for drawing a line from a slope and an intercept#5668kirthi-b wants to merge 1 commit into
kirthi-b wants to merge 1 commit into
Conversation
Figure.add_abline(slope=1, intercept=0, ...) draws a straight line the way matplotlib's axline or R's abline do, computing the line's endpoints from the current x-axis range (or, if that range hasn't been set explicitly, from the data already plotted on that axis) at the time it is called. The method mirrors the signature style of add_hline/add_vline: slope and intercept play the role of y/x, and row, col, exclude_empty_subplots, annotation and annotation_* all behave the same way. Annotation placement reuses the existing vline positioning logic in shapeannotation.py, which was already written generically enough to place text relative to a slanted line's endpoints. Unlike add_hline/add_vline, the new line is not anchored to the plot edges: its endpoints are ordinary data coordinates fixed at add time, so panning or zooming afterwards won't move or extend it. This is called out explicitly in the docstring and in the shapes doc page. Closes plotly#3166
Contributor
|
Thanks for the PR! Could you please update the PR description to use the template? We need to see screenshots/videos along with testing instructions. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Link to issue
Closes #3166
Description of change
Adds
Figure.add_abline(slope=..., intercept=...), which draws a straight line from a slope and an intercept the way matplotlib'saxlineor R'sablinedo. The signature and behavior followadd_hlineandadd_vline, includingrow,col,exclude_empty_subplots,annotation, and theannotation_*keywords, and extra kwargs are forwarded toadd_shape.Demo
Left panel is a 1:1 reference line on a predicted vs actual plot, right panel is a least squares fit drawn from its own slope and intercept. Both use the annotation keywords, and both are targeted at a specific subplot with
rowandcol.Testing strategy
New tests in
tests/test_optional/test_autoshapes/test_add_abline.pycover basic usage, a custom slope and intercept, the priority of an explicit axis range over a data derived range, the fallback when there is no data and no range, subplot targeting withrowandcol,exclude_empty_subplots, and the annotation keywords. To try it by hand, check out the branch and run the snippet above, then runpytest tests/test_optional/test_autoshapes/test_add_abline.py.Additional information (optional)
One behavior is worth calling out, since it came up in the issue discussion about "infinite" lines.
add_hlineandadd_vlinestay pinned to the plot edges through pan and zoom because they use plotly's axisdomainreference for one dimension, which plotly.js recalculates against the visible range on every render. A line with an arbitrary slope cannot use that trick, because domain references only work when one axis is held constant, not when both move together.So
add_ablinecomputes its endpoints once, at the moment it is called, from the axis's current range, or from the data already plotted if no range has been set. Those endpoints are then ordinary fixed data coordinates like any shape added withadd_shape. If you pan or zoom afterwards, or add data outside the original range, the line will not move or extend to follow. This is documented in the docstring and on the shapes doc page, and callingadd_ablineagain picks up the new range.Annotation placement reuses the existing vline positioning code in
shapeannotation.py, which was already written generically enough to handle a slanted line's endpoints (its own comment says as much).Guidelines