From f6763f5a077c361cae81b56db7dfc07561c4c154 Mon Sep 17 00:00:00 2001 From: maxhelskens Date: Thu, 12 Apr 2018 09:38:58 +0200 Subject: [PATCH] finished boxplot --- .gitignore | 3 +- opengrid/library/plotting.py | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1324af9..6ad77c4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ opengrid/notebooks/index.html dist/ build/ -opengrid.egg-info/ \ No newline at end of file +opengrid.egg-info/ +.DS_Store diff --git a/opengrid/library/plotting.py b/opengrid/library/plotting.py index 722d65c..ac32d6b 100644 --- a/opengrid/library/plotting.py +++ b/opengrid/library/plotting.py @@ -1,5 +1,6 @@ import matplotlib.pyplot as plt import matplotlib +import pandas as pd def plot_style(): @@ -13,4 +14,57 @@ def plot_style(): matplotlib.style.use('seaborn-deep') plt.rcParams['figure.figsize'] = 16, 6 + + # To overrule the legend style + plt.rcParams['legend.facecolor'] = "#ffffff" + plt.rcParams['legend.frameon'] = True + plt.rcParams['legend.framealpha'] = 1 + return plt + + +def boxplot(df, plot_mean=False, plot_ids=None): + """ + Plot boxplots + + Plot the boxplots of a dataframe in time + + Parameters + ---------- + df: Pandas Dataframe + Every collumn is a timeseries + plot_mean: bool + Wether or not to plot the means + plot_ids: [str] + List of id's to plot + + Returns + ------- + matplotlib figure + """ + description = df.apply(pd.DataFrame.describe, axis=1) + + # plot + plt = plot_style() + + df.index = df.index.map(lambda x: x.strftime('%b')) + + df = df.T + + fig, ax = plt.subplots() + axes, bp = df.boxplot(ax=ax, return_type='both') + plt.setp(bp['boxes'], color='black') + plt.setp(bp['whiskers'], color='black') + + for id in plot_ids: + ax.scatter(x=axes.get_xticks(), y=df.loc[id], label=str(id)) + + if plot_mean: + ax.scatter(x=axes.get_xticks(), y=description['mean'], label="Mean", color='k', s=30, marker='+') + + plt.xticks(rotation=45) + + ax.legend() + + return fig +