Search code examples
pythonplotlyscatter

How to extract R-squared from plotly px.scatter trendline result?


how can I extract R-squared value from the following summary():

import plotly.express as px
df = ...
fig = px.scatter(df, x="...", y="...", trendline="ols") 
a = px.get_trendline_results(fig).px_fit_results.iloc[0].summary()

I know how to extract coefficients of trendline with params:coef in the picture

print(fit_results.params)

but I need to extract R-squared value from the results to annotate it in static saved figs.


Solution

  • I found it :)

    a = px.get_trendline_results(fig).px_fit_results.iloc[0].rsquared
    

    that's it.