Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using OLS trendline on single-point data #2549

Closed
slishak opened this issue Jun 8, 2020 · 3 comments · Fixed by #2554
Closed

Error when using OLS trendline on single-point data #2549

slishak opened this issue Jun 8, 2020 · 3 comments · Fixed by #2554

Comments

@slishak
Copy link

slishak commented Jun 8, 2020

This could be an issue with statsmodels rather than Plotly, but when trying to fit trendlines to a line which only contains one repeated point, only one parameter is returned from the statsmdels fit and an error occurs when trying to build the hover header for the trendline.

import pandas as pd
import numpy as np
import plotly.express as px

df = pd.DataFrame({'y': np.zeros(2), 'x': np.ones(2)})
fig = px.scatter(df, x='x', y='y', trendline='ols')
Traceback (most recent call last):
  File "C:\Users\slishak\AppData\Local\Programs\Python\Python38\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-17-ef61e6ef2734>", line 1, in <module>
    fig = px.scatter(df, x='y', y='x', trendline='ols')
  File "C:\Users\slishak\AppData\Local\Programs\Python\Python38\lib\site-packages\plotly\express\_chart_types.py", line 62, in scatter
    return make_figure(args=locals(), constructor=go.Scatter)
  File "C:\Users\slishak\AppData\Local\Programs\Python\Python38\lib\site-packages\plotly\express\_core.py", line 1907, in make_figure
    patch, fit_results = make_trace_kwargs(
  File "C:\Users\slishak\AppData\Local\Programs\Python\Python38\lib\site-packages\plotly\express\_core.py", line 298, in make_trace_kwargs
    fit_results.params[1],
IndexError: index 1 is out of bounds for axis 0 with size 1

Versions:

plotly==4.8.0
statsmodels==0.11.1
@nicolaskruchten
Copy link
Contributor

Thanks for reporting this! I think this is a regression and we'll get it fixed shortly :)

@nicolaskruchten
Copy link
Contributor

Ah, I spoke too soon: not a regression. But we'll still fix it :)

@nicolaskruchten
Copy link
Contributor

Clarification: the problem here is that when the slope is zero, statsmodels returns only the intercept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants