Skip to content

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

Closed
@slishak

Description

@slishak

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions