Skip to content

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

Open

Description

@johann-petrak

Currently there seems to be no way to add a line to an existing graph (e.g. as a new trace) based on slope and intercept and without the necessity to find out the actual ranges of x and y axes of the figure.
All examples I have seen so far are based on using existing x values of dots in a scatter plot, calculating the corresponding y values for the line and adding those line segments in a new scatter plot trace.

But this has two huge disadvantages:

  • the y values have to get calculated for those x values (which may be a lot) or x values need to get selected
  • if a lot of segmets are calculated, the graph complexity increases a lot as well

But plotting a line based on just slope and intercept is probably the second most easy thing to do, so there should be a way to add a line that way.

The basic signature of the corresponding method or class could be similar to the corresponding R function:
ABline(a=intercept, b=slope, h=yforhorizontallines, v=xforverticallines)

Activity

  1. johnc1231 commented on Dec 8, 2021

    @johnc1231

    Seconded. I am looking for way to draw an infinite line, and there doesn't seem to be any possible way to do so currently.

  2. brianstthomas commented on Sep 9, 2022

    @brianstthomas

    This would be really useful - this often comes up when the options for controlling the trendline through px.scatter don't enable retrieving the a + bx line we're trying to show.

  3. ceandrade commented on Mar 10, 2023

    @ceandrade

    I also second here. This functionality is just handy in many several cases. And ultimately, this is a simple vector, which should be easy to draw and scale. The px.scatter solutions over StackOverflow are basic "manual" rasterization. In other words, we are using raster images where vector images are perfectly acceptable and fine (and probably much faster and less data-intensive).

  4. johann-petrak commented on Mar 10, 2023

    @johann-petrak
    Author

    (and probably much faster and less data-intensive)

    Extremely important aspect for java-script based renderers, where all the data points of that line have to get stored in the browser, instead of a single geometric object. In a large graph with high resolution the workaround is painfully inefficient.

    The hvplot/Bokeh library has proper graph elements like line, circle etc for such things.

  5. j-at-ch commented on Jun 7, 2024

    @j-at-ch

    +1 on this issue.

    I can add a sketch func that indirectly avoids "examples I have seen so far are based on using existing x values of dots in a scatter plot" - but it's certainly not suitable for production in its current form.

    import plotly.express as px
    import plotly.graph_objects as go
    
    
    def add_abline(a: float, b: float, fig: go.Figure, inplace=False) -> go.Figure:
        x0, x1 = fig.full_figure_for_development(warn=False).layout.xaxis.range  # obv. avoid in production
        
        fig_out = px.line(
            x=[x0, x1],
            y=[a + b * x0, a + b * x1]
        )
        
        if inplace:
            fig_out = fig.add_traces(
                fig_out.data
            )
        return fig_out

    I'd also much appreciate a library solution, Plotly team, if you read this :)

  6. gvwilson commented on Jul 11, 2024

    @gvwilson
    Contributor

    Hi - we are tidying up stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for a while, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. If you'd like to submit a PR, we'd be happy to prioritize a review, and if it's a request for tech support, please post in our community forum. Thank you - @gvwilson

  7. johann-petrak commented on Jul 11, 2024

    @johann-petrak
    Author

    This is an unsolved issue and in comparison to other plotting libraries (e.g. matplotlib) a serious lack of functionality: please read the previous comments why.
    In reality it is not just about lines but other kinds of curves or graphics primitives one may want to add to plot.

  8. janosh commented on Jul 11, 2024

    @janosh
    Contributor

    strongly agree, this is high prio and should not be closed

  9. gvwilson commented on Jul 11, 2024

    @gvwilson
    Contributor

    thanks @johann-petrak and @janosh - I'll reopen and add to our backlog but realistically it will be months before we can get to it. If either of you would like to submit a PR, I'll prioritize reveiw.

  10. self-assigned this
    on Jul 11, 2024
  11. removed their assignment
    on Aug 2, 2024
  12. added a commit that references this issue on Aug 6, 2026
    4b74b49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3backlogfeaturesomething new

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions