Hi Folks -
The community has been very helpful so I thought I would give back with a tip.
If you’ve used Python to plot out linear best fits using the process mentioned in the plotly API library (plot.ly https://plot.ly/python/linear-fits/), you will notice the annotations are manual inputs. This could be problematic if your data changes.
Current method:
annotation = go.Annotation(
x=3.5,
y=23.5,
text=’$R^2 = 0.9551,\Y = 0.716X + 19.18$’,
showarrow=False,
font=go.Font(size=16)
)
If your data changes, to get the annotations to follow suit without manual intervention, you could use the stats package (https://docs.scipy.org/doc/scipy/reference/stats.html) to define properties of your line of best fit based on the data. Like this:
slope, intercept, r_value, p_value, std_err = stats.linregress(df[df.columns[2]],df[df.columns[0]])
line = slope*df[df.columns[2]]+intercept
rval = str(round(r_value**2,2))
slp = str(round(slope, 4))
intr = str(round(intercept, 2))
From there you can define your annotations based on the variables:
annotation = go.Annotation(
x=50,
y=20,
text="R squared = " + rval + “, y =” + slp + “x+” + intr,
showarrow=False,
font=go.Font(size=16)
)
Let me know if you have questions - hope this helps!