Skip to content

Commit e7f04b1

Browse files
ols log options checkpoint
1 parent f0bbf60 commit e7f04b1

File tree

1 file changed

+12
-3
lines changed
  • packages/python/plotly/plotly/express

1 file changed

+12
-3
lines changed

packages/python/plotly/plotly/express/_core.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,19 @@ def ols(options, x, y, x_label, y_label, non_missing):
266266
import statsmodels.api as sm
267267

268268
add_constant = options.get("add_constant", True)
269-
fit_results = sm.OLS(
270-
y, sm.add_constant(x) if add_constant else x, missing="drop"
271-
).fit()
269+
log_x = options.get("log_x", False)
270+
log_y = options.get("log_y", False)
271+
272+
if log_y:
273+
y = np.log(y)
274+
if log_x:
275+
x = np.log(x)
276+
if add_constant:
277+
x = sm.add_constant(x)
278+
fit_results = sm.OLS(y, x, missing="drop").fit()
272279
y_out = fit_results.predict()
280+
if log_y:
281+
y_out = np.exp(y_out)
273282
hover_header = "<b>OLS trendline</b><br>"
274283
if len(fit_results.params) == 2:
275284
hover_header += "%s = %g * %s + %g<br>" % (

0 commit comments

Comments
 (0)