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 — Smoothing in Python In [1]:
import plotly.graph_objects as go import numpy as np import pandas as pd import scipy from scipy import signal Savitzky-Golay Filter¶
There is reason to smooth data if there is little to no small-scale structure in the data. The danger to this thinking is that one may skew the representation of the data enough to change its perceived meaning, so for the sake of scientific honesty it is an imperative to at the very minimum explain one's reason's for using a smoothing algorithm to their dataset. In this example we use the Savitzky-Golay Filter, which fits subsequent windows of adjacent data with a low-order polynomial. In [2]:
import plotly.graph_objects as go import numpy as np import pandas as pd import scipy from scipy import signal np.random.seed(1) x = np.linspace(0, 10, 100) y = np.sin(x) noise = 2 * np.random.random(len(x)) - 1 # uniformly distributed between -1 and 1 y_noise = y + noise fig = go.Figure() fig.add_trace(go.Scatter( x=x, y=y, mode='markers', marker=dict(size=2, color='black'), name='Sine' )) fig.add_trace(go.Scatter( x=x, y=y_noise, mode='markers', marker=dict( size=6, color='royalblue', symbol='circle-open' ), name='Noisy Sine' )) fig.add_trace(go.Scatter( x=x, y=signal.savgol_filter(y_noise, 53, # window size used for filtering 3), # order of fitted polynomial mode='markers', marker=dict( size=6, color='mediumpurple', symbol='triangle-up' ), name='Savitzky-Golay' )) fig.show() Triangular Moving Average¶Another method for smoothing is a moving average. There are various forms of this, but the idea is to take a window of points in your dataset, compute an average of the points, then shift the window over by one point and repeat. This will generate a bunch of points which will result in the Let us look at the common In the In [3]:
def smoothTriangle(data, degree): triangle=np.concatenate((np.arange(degree + 1), np.arange(degree)[::-1])) # up then down smoothed=[] for i in range(degree, len(data) - degree * 2): point=data[i:i + len(triangle)] * triangle smoothed.append(np.sum(point)/np.sum(triangle)) # Handle boundaries smoothed=[smoothed[0]]*int(degree + degree/2) + smoothed while len(smoothed) < len(data): smoothed.append(smoothed[-1]) return smoothed fig = go.Figure() fig.add_trace(go.Scatter( x=x, y=y, mode='markers', marker=dict( size=2, color='rgb(0, 0, 0)', ), name='Sine' )) fig.add_trace(go.Scatter( x=x, y=y_noise, mode='markers', marker=dict( size=6, color='#5E88FC', symbol='circle-open' ), name='Noisy Sine' )) fig.add_trace(go.Scatter( x=x, y=smoothTriangle(y_noise, 10), # setting degree to 10 mode='markers', marker=dict( size=6, color='#C190F0', symbol='triangle-up' ), name='Moving Triangle - Degree 10' )) fig.show() 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. |