Skip to content

Commit f036e5c

Browse files
ols log options checkpoint
1 parent 1dc6ab3 commit f036e5c

File tree

1 file changed

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

1 file changed

+12
-3
lines changed

Diff for: packages/python/plotly/plotly/express/_core.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,19 @@ def ols(options, x, y, x_label, y_label, non_missing):
239239
import statsmodels.api as sm
240240

241241
add_constant = options.get("add_constant", True)
242-
fit_results = sm.OLS(
243-
y, sm.add_constant(x) if add_constant else x, missing="drop"
244-
).fit()
242+
log_x = options.get("log_x", False)
243+
log_y = options.get("log_y", False)
244+
245+
if log_y:
246+
y = np.log(y)
247+
if log_x:
248+
x = np.log(x)
249+
if add_constant:
250+
x = sm.add_constant(x)
251+
fit_results = sm.OLS(y, x, missing="drop").fit()
245252
y_out = fit_results.predict()
253+
if log_y:
254+
y_out = np.exp(y_out)
246255
hover_header = "<b>OLS trendline</b><br>"
247256
if len(fit_results.params) == 2:
248257
hover_header += "%s = %g * %s + %g<br>" % (

0 commit comments

Comments
 (0)