Skip to content

Instantly share code, notes, and snippets.

@LeonardAukea
Last active September 18, 2018 18:16
Show Gist options
  • Save LeonardAukea/33fa6a90de639da904386f704467dbd4 to your computer and use it in GitHub Desktop.
Save LeonardAukea/33fa6a90de639da904386f704467dbd4 to your computer and use it in GitHub Desktop.
Stacked barchart
def overlapped_bar(df, show=False, width=0.9, alpha=.5,
title='', xlabel='', ylabel='', **plot_kwargs):
"""Like a stacked bar chart except bars on top of each other with transparency"""
xlabel = xlabel or df.index.name
N = len(df)
M = len(df.columns)
indices = np.arange(N)
colors = ['steelblue', 'firebrick', 'darksage', 'goldenrod', 'gray'] * int(M / 5. + 1)
for i, label, color in zip(range(M), df.columns, colors):
kwargs = plot_kwargs
kwargs.update({'color': color, 'label': label})
plt.bar(indices, df[label], width=width, alpha=alpha if i else 1, **kwargs)
plt.xticks(indices + .5 * width,
['{}'.format(idx) for idx in df.index.values])
plt.legend()
plt.title(title)
plt.xlabel(xlabel)
if show:
plt.show()
return plt.gcf()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment