Last active
January 12, 2021 15:27
-
-
Save justinhchae/9a0e99b63b242aaa008e27175c8752ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import plotly.graph_objects as go | |
import plotly_express as px | |
# group the dataframe | |
group = df.groupby('types') | |
# create a blank canvas | |
fig = go.Figure() | |
# each group iteration returns a tuple | |
# (group name, dataframe) | |
for group_name, df in group: | |
# in each loop, draw a time series then a regression line | |
fig.add_trace( | |
go.Scatter( | |
x=df['dates'] | |
, y=df['count'] | |
, fill='tozeroy' | |
)) | |
# source: https://stackoverflow.com/questions/60204175/plotly-how-to-add-trendline-to-a-bar-chart | |
# generate a regression line with px | |
help_fig = px.scatter(df, x=df['dates'], y=df['count'], trendline="lowess") | |
# extract points as plain x and y | |
x_trend = help_fig["data"][1]['x'] | |
y_trend = help_fig["data"][1]['y'] | |
# add the x,y data as a scatter graph object | |
fig.add_trace( | |
go.Scatter(x=x_trend, y=y_trend, name='trend')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment