# Plotly Python: how to add a line with given slope and intercapt to a given x/y plot?

Is there an easy way to just add a line to a plot by specifying the slope and intercept in the current diagram’s x/y coordinates?

I know one can do this by adding a scatter trace with line mode and either finding the min/max (x,y) points or just adding the ys for all the xs one has for the line, but in some cases all one has is the slope and intercept and doing this “manually” every time is just such a waste of time.

``````import matplotlib.pyplot as plt
import numpy as np

def abline(slope, intercept):
"""Plot a line from slope and intercept"""
axes = plt.gca()
x_vals = np.array(axes.get_xlim())
y_vals = intercept + slope * x_vals
plt.plot(x_vals, y_vals, '--')
``````

This is a manual method for doing it with matplotlib, not plotly. I really cannot see how it is relevant to my question.

‘manual’ ? - there is no paper and pencil involved in the response above. it is written in python code.
the idea was not to complete the plot in matplotlib, but to modify the function in a relatively simple way to produce the points that constitute the straight line, defined by slope and intercept, within the upper and lower x-bounds of the data set.
once the pair of (x, y) points are defined, they can be added as a line to whatever scatter plot is otherwise begin generated.
slight modification to above function can likely be made to produce a single line re-usable method call.

“Manual” meaning that one has to write code where there should be a library function or other simple option to do it.

But anyways, again, this is the Plotly forum, my question is about Plotly, I cannot see how a copy of a stackoverflow answer about matplotlib is relevant here.

@johann.petrak here is a solution that works for me:

``````import plotly.express as px
import plotly.graph_objects as go

fig = px.scatter(data, 'x', 'y')
go.Scatter(x=data['x'], y=0.5*data['x'], name="slope", line_shape='linear')
)
fig.show()
``````

This is what I already know: plotting line segments for each value in x which seems like a really bad way to do it.
Other plot libraries have an abline method which will simple add the necessary line using the only two parameters needed: intercept and slope, not require to calculate an x/y pair for each point in the graph.

Hi Johan did you finally find a concise method to achieve that ?
I’m tired of reconstructing lines from intercept and slope but I really want to stay on Plotly.

Regards

2 Likes

Sorry for the late reply, I did not use plotly for quite a while.

I have not seen a concise method for this so far, sadly. I think it is really absurd that such a basic tool is not present. Drawing a line by slope and intercept should be embarrassingly easy to implement really BTW I opened an issue back then which is still open:

I explained my approach here: charts - How can I add the slope of a specific point in a polynomial line in plotly - Stack Overflow

It is not that easy but doable. Feel free to comment on stackoverflow. Hope this helps