Skip to content

Instantly share code, notes, and snippets.

@fclesio
Created October 29, 2019 16:23
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 fclesio/ce14a5fdc22dc11a6ab9aaff902ce91c to your computer and use it in GitHub Desktop.
Save fclesio/ce14a5fdc22dc11a6ab9aaff902ce91c to your computer and use it in GitHub Desktop.
# Some graphs with rolling average
date_range = [2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010, 2011, 2012,
2013, 2014, 2015, 2016, 2017,
2018]
def get_graph_ts_rolling_average(ts, title, window, date_range=date_range):
"""Generate graph of a Time Series with a simple rolling average
Parameters
----------
ts : Pandas Dataframe column
Dataframe column with a metric to be ploted
title : Pandas Dataframe
Graph title to be displayed
window : int
Rolling back window to be considered in the average
date_range : Array
Array to be used in the ploting. Matplotlib has a
very bad way to deal with that, so I need to use this
workaround to place all years properly
Returns
-------
"""
plt.figure(figsize=(20,10))
plt.plot(date_range, ts.rolling(window=window, center=False).mean(), label='gini');
plt.title(f'{title}{window}')
plt.xlabel('Years')
plt.ylabel('Gini Index')
plt.xticks(gini_df.index)
plt.legend()
get_graph_ts_rolling_average(gini_df['gini'],
'Rolling Average in Gini Index in Brasileirão - Rolling Average window=',
3,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment