Skip to content

Add add_abline for drawing a line from a slope and an intercept#5668

Open
kirthi-b wants to merge 1 commit into
plotly:mainfrom
kirthi-b:feat/add-abline
Open

Add add_abline for drawing a line from a slope and an intercept#5668
kirthi-b wants to merge 1 commit into
plotly:mainfrom
kirthi-b:feat/add-abline

Conversation

@kirthi-b

Copy link
Copy Markdown

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.

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
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

Successfully merging this pull request may close these issues.

Implement the equivalent of abline (plot line using slope/intercept only) to add line to graph

1 participant