Skip to content

Instantly share code, notes, and snippets.

@arthurcgusmao
Created May 15, 2019 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save arthurcgusmao/39905499340d28fa59834f652905dcec to your computer and use it in GitHub Desktop.
Save arthurcgusmao/39905499340d28fa59834f652905dcec to your computer and use it in GitHub Desktop.
Seasonal Plot in Python using Pandas and Seaborn
import pandas as pd
import seaborn as sns
def seasonal_plot(df, season='year', index='month', column=None):
"""Makes a seasonal plot of one column of the input dataframe. Considers the first columns by default.
Arguments:
- df (Pandas DataFrame): DataFrame indexed by Datetime (see `parse_dates` parameter when reading a CSV);
- season (string): the season that you want to considering when doing the plot, e.g., year, month, etc.;
- index (string): corresponds to the X axis of the plot. You should choose based on the index period that you're using;
- column (string, optional): the DataFrame column to consider. Picks the first one by default.
"""
if column == None:
column = df.columns[0]
piv_index = getattr(df.index, index)
piv_season = getattr(df.index, season)
piv = pd.pivot_table(df, index=piv_index, columns=piv_season, values=[column])
piv.plot(figsize=(12,8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment