Last active
August 29, 2015 14:08
-
-
Save bjacobowski/6c6d1ad0af8d50694765 to your computer and use it in GitHub Desktop.
Parallel Coordinates - Seasonality Example
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
""" | |
parallel_coordinates seasonality example | |
""" | |
import math | |
import datetime as dt | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from pandas.tools.plotting import parallel_coordinates | |
pd.options.display.mpl_style = 'ggplot' | |
randn = np.random.randn | |
ix = pd.date_range(dt.date(2011,1,1), dt.date(2014,12,31)) | |
n = len(ix) | |
z = np.linspace(0, math.pi * 2 * len(set(ix.year)), n) | |
y = np.sin(z) | |
df = pd.DataFrame({'var': y*(1+randn(n))}, index=ix) | |
df['yr'] = df.index.year | |
df['day'] = df.groupby('yr').cumcount().add(1) | |
pvt = pd.pivot_table(df, index='yr', columns='day')['var'] | |
pvt = pvt.reset_index() | |
fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(6,8)) | |
df['var'].plot(ax=ax[0]) | |
parallel_coordinates(pvt, 'yr', ax=ax[1]) | |
parallel_coordinates(pvt, 'yr', ax=ax[2], axvlines=False) | |
ttl = ['var', | |
'parallel_coordinates w/ axvlines', | |
'parallel_coordinates, no axvlines'] | |
[ax[i].set_title(x) for i, x in enumerate(ttl)] | |
[ax[i].legend(loc=1, prop={'size': 10}) for i in range(3)] | |
[ax[i].tick_params(axis='both', which='major', labelsize=10) for i in range(3)] | |
st, end = ax[1].get_xlim() | |
xticks = range(int(st), int(end), 30) | |
ax[1].xaxis.set_ticks(xticks) | |
ax[1].set_xticklabels(xticks) | |
ax[2].xaxis.set_ticks(xticks) | |
ax[2].set_xticklabels(xticks) | |
fig.subplots_adjust(hspace=0.33) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment