GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing plotly.com — Linear and non-linear trendlines in Python Linear fit trendlines with Plotly Express¶Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures. Plotly Express allows you to add Ordinary Least Squares regression trendline to scatterplots with the In [1]:
import plotly.express as px df = px.data.tips() fig = px.scatter(df, x="total_bill", y="tip", trendline="ols") fig.show() Fitting multiple lines and retrieving the model parameters¶Plotly Express will fit a trendline per trace, and allows you to access the underlying model parameters for all the models. In [2]:
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() sex smoker px_fit_results 0 Female No <statsmodels.regression.linear_model.Regressio... 1 Female Yes <statsmodels.regression.linear_model.Regressio... 2 Male No <statsmodels.regression.linear_model.Regressio... 3 Male Yes <statsmodels.regression.linear_model.Regressio... Out[2]:
Notes: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified. Displaying a single trendline with multiple traces¶new in v5.2 To display a single trendline using the entire dataset, set the In [3]:
import plotly.express as px df = px.data.tips() fig = px.scatter(df, x="total_bill", y="tip", symbol="smoker", color="sex", trendline="ols", trendline_scope="overall") fig.show() In [4]:
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", trendline_scope="overall", trendline_color_override="black") fig.show() OLS Parameters¶new in v5.2 OLS trendlines can be fit with log transformations to both X or Y data using the In [5]:
import plotly.express as px df = px.data.gapminder(year=2007) fig = px.scatter(df, x="gdpPercap", y="lifeExp", trendline="ols", trendline_options=dict(log_x=True), title="Log-transformed fit on linear axes") fig.show() In [6]:
import plotly.express as px df = px.data.gapminder(year=2007) fig = px.scatter(df, x="gdpPercap", y="lifeExp", log_x=True, trendline="ols", trendline_options=dict(log_x=True), title="Log-scaled X axis and log-transformed fit") fig.show() Locally Weighted Scatterplot Smoothing (LOWESS)¶Plotly Express also supports non-linear LOWESS trendlines. In order use this feature, you will need to install In [7]:
import plotly.express as px df = px.data.stocks(datetimes=True) fig = px.scatter(df, x="date", y="GOOG", trendline="lowess") fig.show() new in v5.2 The level of smoothing can be controlled via the In [8]:
import plotly.express as px df = px.data.stocks(datetimes=True) fig = px.scatter(df, x="date", y="GOOG", trendline="lowess", trendline_options=dict(frac=0.1)) fig.show() Moving Averages¶new in v5.2 Plotly Express can leverage Pandas' In [9]:
import plotly.express as px df = px.data.stocks(datetimes=True) fig = px.scatter(df, x="date", y="GOOG", trendline="rolling", trendline_options=dict(window=5), title="5-point moving average") fig.show() In [10]:
import plotly.express as px df = px.data.stocks(datetimes=True) fig = px.scatter(df, x="date", y="GOOG", trendline="ewm", trendline_options=dict(halflife=2), title="Exponentially-weighted moving average (halflife of 2 points)") fig.show() In [11]:
import plotly.express as px df = px.data.stocks(datetimes=True) fig = px.scatter(df, x="date", y="GOOG", trendline="expanding", title="Expanding mean") fig.show() Other Functions¶The In [12]:
import plotly.express as px df = px.data.stocks(datetimes=True) fig = px.scatter(df, x="date", y="GOOG", trendline="rolling", trendline_options=dict(function="median", window=5), title="Rolling Median") fig.show() In [13]:
import plotly.express as px df = px.data.stocks(datetimes=True) fig = px.scatter(df, x="date", y="GOOG", trendline="expanding", trendline_options=dict(function="max"), title="Expanding Maximum") fig.show() In some cases, it is necessary to pass options into the underying Pandas function, for example the In [14]:
import plotly.express as px df = px.data.stocks(datetimes=True) fig = px.scatter(df, x="date", y="GOOG", trendline="rolling", trendline_options=dict(window=5, win_type="gaussian", function_args=dict(std=2)), title="Rolling Mean with Gaussian Window") fig.show() Displaying only the trendlines¶In some cases, it may be desirable to show only the trendlines, by removing the scatter points. In [15]:
import plotly.express as px df = px.data.stocks(indexed=True, datetimes=True) fig = px.scatter(df, trendline="rolling", trendline_options=dict(window=5), title="5-point moving average") fig.data = [t for t in fig.data if t.mode == "lines"] fig.update_traces(showlegend=True) #trendlines have showlegend=False by default fig.show() In [ ]:
What About Dash?¶Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library. Learn about how to install Dash at https://dash.plot.ly/installation. Everywhere in this page that you see import plotly.graph_objects as go # or plotly.express as px fig = go.Figure() # or any Plotly Express function e.g. px.bar(...) # fig.add_trace( ... ) # fig.update_layout( ... ) from dash import Dash, dcc, html app = Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |