DGomonov
DGomonov

Reputation: 845

Python/Plotly: How to print y-trend values from LOWESS line?

Here is what my snippet of LOWESS Smoothing looks like when I use Plotly lib: LOWESS Smoothing line

Now, I know that using Plotly it provides me the y-trend values if I hover over the line: enter image description here

Problem: I do not know how to print out these trend values.

I am aware there is a function px.get_trendline_results(forecast_fig) but it prints out nothing for me...

use of known function

Help is really appreciated!

Upvotes: 2

Views: 1725

Answers (1)

nicolaskruchten
nicolaskruchten

Reputation: 27410

Today these values are not available but they could be in a future version! The reason nothing comes out of get_trendline_result is that LOWESS has no underlying model, it’s just a kind of average.

Under the hood Plotly Express uses the statsmodels library to generate LOWESS curves so you can probably run the same analysis outside of PX for now.

Here is the code PX uses: https://github.com/plotly/plotly.py/blob/3ca829c73bd4841666c8b810f5e8457514eb3c99/packages/python/plotly/plotly/express/_core.py#L232

If you want to query the actual values out of the figure, and you have nothing mapping to color or symbol then you can do something like:

import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", trendline="lowess")
print(fig.data[1].y)

but if you have color set, then the actual trace index in fig.data[ <index> ] will vary so this is pretty brittle.

Upvotes: 2

Related Questions