Skip to content

Instantly share code, notes, and snippets.

@Barry1
Created May 28, 2020 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Barry1/897b0b8f3d9d31f15aaed48849d27df0 to your computer and use it in GitHub Desktop.
Save Barry1/897b0b8f3d9d31f15aaed48849d27df0 to your computer and use it in GitHub Desktop.
plot two columns against each other with local uncertainty bands by windowing
import pandas
def plotwithuncertainty(data: pandas.DataFrame,
x: str,
y: str,
windowlen: int = max(10, round(len(data) / 100)),
**pltops) -> None:
evalobj = data[[x, y]].reset_index().rolling(
window=windowlen, center=True, on=x)
wrkset = pandas.DataFrame()
wrkset.loc[:, 'meanx'] = evalobj[x].mean()
wrkset.loc[:, 'meany'] = evalobj[y].mean()
wrkset.loc[:, 'stdy'] = evalobj[y].std()
wrkset.loc[:, 'lb'] = wrkset['meany'] - 3 * wrkset['stdy']
wrkset.loc[:, 'ub'] = wrkset['meany'] + 3 * wrkset['stdy']
wrkset[['meanx', 'lb', 'meany', 'ub']].sort_values(
'meanx').plot(x='meanx', **pltops)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment