Last active
December 14, 2023 22:37
-
-
Save camriddell/643ed8380f1ff697321f0eddf5b2b0e3 to your computer and use it in GitHub Desktop.
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
from matplotlib.pyplot import show, rc, figure | |
from matplotlib.dates import AutoDateLocator, ConciseDateFormatter, date2num | |
from numpy import where | |
from pandas import read_parquet | |
from pandas.tseries.offsets import DateOffset | |
revenue = ( | |
read_parquet('data/revenues.parquet')['revenue'] | |
) | |
rc('font', size=14) | |
rc('axes.spines', top=False, bottom=False) | |
def split_ax(ax): | |
bbox = ax.get_position() | |
bottom, _, top = bbox.splity(.2, .3) | |
ax.set_position(top) | |
ax2 = fig.add_axes(bottom) | |
ax.xaxis.set_tick_params(labelbottom=False, bottom=False) | |
ax2.xaxis.set_tick_params(labelbottom=False, bottom=False, labeltop=True) | |
ax.sharex(ax2) | |
return ax, ax2 | |
fig = figure(figsize=(12, 8), dpi=200) | |
gs = fig.add_gridspec(2, 2, height_ratios=[2, 1], hspace=.3, wspace=.05, top=.92) | |
decline_top, decline_bottom = split_ax(fig.add_subplot(gs[0, 0])) | |
recent_top, recent_bottom = split_ax(fig.add_subplot(gs[0, 1])) | |
overall_ax = fig.add_subplot(gs[1, :]) | |
decline_top.sharey(recent_top) | |
decline_bottom.sharey(recent_bottom) | |
to_plot = [ | |
[(decline_top, decline_bottom), ('2017-01', '2017-06')], | |
[(recent_top, recent_bottom), ('2018-01', '2018-06')] | |
] | |
for (top, bottom), (start, stop) in to_plot: | |
pdata = revenue.loc[start:stop] | |
top.plot(pdata.index, pdata) | |
daily_revenue = pdata.diff() | |
bottom.bar( | |
daily_revenue.index, daily_revenue, | |
color=where(daily_revenue > 0, 'tab:green', 'tab:red') | |
) | |
overall_ax.plot(revenue.index, revenue) | |
for ax in [decline_top, recent_top, overall_ax]: | |
loc = AutoDateLocator() | |
ax.xaxis.set_major_locator(loc) | |
ax.xaxis.set_major_formatter(ConciseDateFormatter(loc)) | |
ax.yaxis.set_major_formatter('${x:.1f}M') | |
ax.set_ylabel('Cumulative') | |
ax.margins(x=0) | |
for ax in [decline_bottom, recent_bottom]: | |
ax.yaxis.set_major_formatter('${x:.1f}M') | |
ax.set_ylabel('Daily') | |
ax.xaxis_date() | |
ax.margins(x=0) | |
recent_bottom.set_ylabel('') | |
recent_top.set_ylabel('') | |
recent_top.yaxis.set_tick_params(left=False, labelleft=False) | |
recent_bottom.yaxis.set_tick_params(left=False, labelleft=False) | |
from matplotlib.patches import ConnectionPatch | |
for ax in [decline_bottom, recent_bottom]: | |
for i, x in enumerate(ax.get_xlim()): | |
conn = ConnectionPatch( | |
xyA=(x, 1), coordsA=overall_ax.get_xaxis_transform(), | |
xyB=(i, 0), coordsB=ax.transAxes | |
) | |
fig.add_artist(conn) | |
overall_ax.axvspan(*ax.get_xlim(), 0, 1, color='yellow', alpha=.3) | |
fig.suptitle('Zooming in on Time Series Data') | |
fig.savefig('timeseries_zoom.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment