avatarRenee LIN
# Summary

The webpage explains how to add trendlines to scatter plots in Plotly, both by using Plotly's built-in function and by manually creating a linear regression model.

# Abstract

The article titled "How to Fit a Trendline in a Scatter Plot in Plotly" provides a comprehensive guide on adding trendlines to scatter plots within Plotly. It starts by acknowledging the ease of creating trendlines with Plotly's built-in function for simple cases and references the official documentation for a detailed walkthrough. For more complex scenarios involving multi-plots or subplots where Plotly does not provide a direct trendline feature, the article demonstrates how to calculate and plot a trendline manually using a linear regression model from the `sklearn` library. The author illustrates the process of creating subplots, fitting a linear regression model to the data, and adding the resulting trendline to the scatter plot. The article concludes by showcasing additional visuals created with Plotly and invites readers to explore more data analysis content on Medium.

# Opinions

- The author suggests that while Plotly makes trendline creation straightforward, there is a lack of built-in functionality for trendlines in multi-plots or subplots.
- The article implies that creating a custom trendline using a linear regression model is a viable and not overly complex alternative when the default functionality is insufficient.
- By providing code snippets and visual examples, the author conveys a practical approach to data visualization, emphasizing the flexibility and power of Plotly combined with additional data analysis tools like `sklearn`.

How to Fit a Trendline in a Scatter Plot in Plotly(default and your own line)

Creating trendlines with Plotly is super easy, but it seems there is no function provided for creating trendlines in multi-plots/subplots. We can just create our own linear regression model to calculate the trendline and plot it.

  1. Plot the trendline using Plotly’s function
  2. Creating our own trendline

1. Plot the trendline using Plotly’s function

This is easy, document link is here

import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", facet_col="smoker", color="sex", trendline="ols")
fig.show()
results = px.get_trendline_results(fig)
print(results)
results.query("sex == 'Male' and smoker == 'Yes'").px_fit_results.iloc[0].summary()
Trendline Created
The line in detail

2. Creating our own trendline

from sklearn import preprocessing
from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=2,subplot_titles=("rmse_v","rmse_v_dif","rmse_s","rmse_a",))
...
...
fig.add_trace(go.Scatter(x=X, y=y, mode=’markers’)) #scatter
model = LinearRegression().fit(X,y)
y_hat = model.predict(X)
fig.add_trace(go.Scatter(x=X, y=y_hat, mode='lines')) #Trendline
...
...

That’s it. Here are other visuals created by Plotly.

  1. Multi-dimensional Data Visualization with Plotly (3D-7D)
  2. How to Create Heatmap with Plotly for Data Analysis

Medium is an open platform where readers find dynamic thinking, and where expert and undiscovered voices can share their writing on any topic. Subscribe to unlock more articles at Medium.

Plotly
Linear Regression
Visualization
Recommended from ReadMedium