Last active
September 18, 2018 18:16
-
-
Save LeonardAukea/33fa6a90de639da904386f704467dbd4 to your computer and use it in GitHub Desktop.
Stacked barchart
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
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