Skip to content

Instantly share code, notes, and snippets.

@VincentTatan
Created May 4, 2020 05:27
Show Gist options
  • Save VincentTatan/515a2e5fbfab23b6c18cad6cabc1a0c7 to your computer and use it in GitHub Desktop.
Save VincentTatan/515a2e5fbfab23b6c18cad6cabc1a0c7 to your computer and use it in GitHub Desktop.
def stack_plot(title, ylabel, dates, val_pairs, ymax=None, legend=None):
# get series values
fig, ax = plt.subplots(1,1)
fig.set_figwidth(20)
fig.set_figheight(10)
ax.set_title(title, size=20)
ax.set_ylabel(ylabel, size=20)
ax.set_xlim((0, len(dates)))
ax.minorticks_on()
ax.grid(which='major', axis='y', linestyle='-', linewidth=2, color='k')
ax.grid(which='minor', axis='y', linestyle='-', linewidth=1, color='.8')
ax.set_axisbelow(True)
ax.tick_params(axis='y', labelsize=16)
totals = np.zeros(len(dates))
for values, label in val_pairs:
ax.bar(np.arange(len(dates))+.5, values, label=label, bottom=totals)
totals += values
date_ticks = np.arange(0, len(dates), 7)
date_labels = dates[date_ticks]
ax.set_xticklabels(date_labels, size=16, rotation=0, horizontalalignment='center')
ax.set_xticks(date_ticks + .5)
if ymax:
ax.set_ylim((0, ymax))
if legend:
ncol, size, loc = legend
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[::-1], labels[::-1], loc=loc, fontsize=size, ncol=ncol, framealpha=1.)
else:
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[::-1], labels[::-1], loc='upper left', fontsize=16, framealpha=1.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment