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
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.
Closes #3166
This adds Figure.add_abline(slope=1, intercept=0, ...), which draws a straight line the way matplotlib's axline or R's abline do. @gvwilson said in the issue thread that a PR would get prioritized review, so here it is.
The signature and behavior mirror add_hline/add_vline as closely as the shape allows: same row, col, exclude_empty_subplots, annotation, and annotation_* handling, same "just forward extra kwargs to add_shape" convention. Annotation placement reuses the existing vline positioning code in shapeannotation.py, which turned out to already be written generically enough to handle a slanted line's endpoints (its own comment says as much).
One thing worth calling out explicitly, since it came up in the issue discussion around "infinite" lines: add_hline and add_vline can stay pinned to the plot edges through pan and zoom because they use plotly's axis "domain" reference for one dimension, which plotly.js recalculates against the currently visible range on every render. A line with an arbitrary slope can't use that trick, because domain references only work when one axis is held constant, not when both axes move together. So add_abline computes its line's endpoints once, at the moment it's called, from the axis's current range (or from the data already plotted, if no range has been set), and those endpoints are then just ordinary fixed data coordinates like any shape added with add_shape. If you pan or zoom afterwards, or add data that falls outside the original range, the line won't move or extend to follow. This is documented in the docstring and in the shapes doc page, and call add_abline again if you want it to reflect a new range.
Tests are added in tests/test_optional/test_autoshapes/test_add_abline.py covering basic usage, custom slope/intercept, explicit-range vs. data-derived range priority, the no-data fallback, subplot row/col targeting, exclude_empty_subplots, and annotation kwargs.