Skip to content

Instantly share code, notes, and snippets.

@macleginn
Created November 21, 2019 12:53
Show Gist options
  • Save macleginn/3924c973dbd74e8e9b320447b92aee4a to your computer and use it in GitHub Desktop.
Save macleginn/3924c973dbd74e8e9b320447b92aee4a to your computer and use it in GitHub Desktop.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# test.csv:
# ,b,c,d
# p,1,2,3
# q,4,5,6
# r,7,8,9
df = pd.read_csv('test.csv', index_col=0)
N = df.shape[1]
fig = plt.figure(figsize=(16,10))
ind = np.arange(N)
layers = []
for i in range(df.shape[0]):
if i == 0:
layers.append(plt.bar(ind, df.iloc[i,:]))
else:
layers.append(plt.bar(ind, df.iloc[i,:], bottom=df.iloc[i-1,:]))
plt.legend(layers, list(df.columns))
plt.xticks(ind, list(df.index))
fig.savefig('stackedbar.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment