plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

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

Open johann-petrak opened this issue 4 years ago • 2 comments

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)

johann-petrak avatar Apr 23 '21 14:04 johann-petrak

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.

johnc1231 avatar Dec 08 '21 20:12 johnc1231

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.

brianstthomas avatar Sep 09 '22 03:09 brianstthomas

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

ceandrade avatar Mar 10 '23 03:03 ceandrade

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

johann-petrak avatar Mar 10 '23 08:03 johann-petrak

+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 :)

j-at-ch avatar Jun 07 '24 09:06 j-at-ch

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

gvwilson avatar Jul 11 '24 13:07 gvwilson

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.

johann-petrak avatar Jul 11 '24 14:07 johann-petrak

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

janosh avatar Jul 11 '24 15:07 janosh

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.

gvwilson avatar Jul 11 '24 16:07 gvwilson