Black Lives Matter. Please consider donating to Black Girls Code today. # 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.