Created
May 15, 2019 16:32
-
-
Save arthurcgusmao/39905499340d28fa59834f652905dcec to your computer and use it in GitHub Desktop.
Seasonal Plot in Python using Pandas and Seaborn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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