100% Stacked Bar Chart In Matplotlib
I'm trying to create a 100% Stacked Bar Chart in MatPlotLib using the College Scorecard data from this site. There are 38 columns that are: Percentage of degrees awarded in [inser
Solution 1:
Firstly, there are a lot of universities in this dataset, maybe a stacked bar plot isn't the best idea?
Anyway, you can loop through each type of degree and add another bar. To create a stacked bar you just change the bottom position of each bar.
import pandas as pd
import matplotlib.pyplot as plt
from cycler import cycler
import numpy as np
df = pd.read_csv('scorecard.csv')
df = df.ix[0:10]
degList = [i for i in df.columns if i[0:4]=='PCIP']
bar_l = range(df.shape[0])
cm = plt.get_cmap('nipy_spectral')
f, ax = plt.subplots(1, figsize=(10,5))
ax.set_prop_cycle(cycler('color',[cm(1.*i/len(degList)) for i inrange(len(degList))]))
bottom = np.zeros_like(bar_l).astype('float')
for i, deg inenumerate(degList):
ax.bar(bar_l, df[deg], bottom = bottom, label=deg)
bottom += df[deg].values
ax.set_xticks(bar_l)
ax.set_xticklabels(df['INSTNM'].values, rotation=90, size='x-small')
ax.legend(loc="upper left", bbox_to_anchor=(1,1), ncol=2, fontsize='x-small')
f.subplots_adjust(right=0.75, bottom=0.4)
f.show()
You can modify this code to get exactly what you want (for example it seems you want percentage rather than fraction, so just multiply each degree column by 100). For testing I took the first 10 universities which results in this plot:
With 10 universities it is already quite a busy plot - with 100 universities it is practically unreadable:
I can guarantee that with almost 8000 universities this stacked bar plot will be completely unreadable. Maybe consider another way to represent the data?


Post a Comment for "100% Stacked Bar Chart In Matplotlib"