Skip to content

Instantly share code, notes, and snippets.

@henryroe
Created June 20, 2014 21:41
Show Gist options
  • Save henryroe/5ad5e0d53e10deefa678 to your computer and use it in GitHub Desktop.
Save henryroe/5ad5e0d53e10deefa678 to your computer and use it in GitHub Desktop.
Plot NSF AST funding data for total funding of grants
max_award = 1e6 # above this amount just bin in to top bin
years_per_bin = 1.0
award_bin_size = 50000
start_year = np.array([a.year + (a.month - 1)/12. for a in awards['StartDate']])
year_bins = np.arange(np.floor(start_year.min()), np.ceil(start_year.max())+1, years_per_bin)
awarded = np.array(awards['AwardedAmountToDate'])
awarded_mean_per_bin = np.zeros(year_bins.size - 1)
awarded_median_per_bin = np.zeros(year_bins.size - 1)
year_per_bin = np.zeros(year_bins.size - 1)
for i in np.arange(year_bins.size - 1):
year_per_bin[i] = (year_bins[i+1] + year_bins[i])/2.0
curmask = (start_year >= year_bins[i]) & (start_year < year_bins[i+1])
awarded_mean_per_bin[i] = awarded[curmask].mean()
awarded_median_per_bin[i] = np.median(awarded[curmask])
awarded[awarded > (max_award - 1.0)] = max_award - 1.0
award_bins = np.arange(0., max_award + 1., award_bin_size)
H, xedges, yedges = np.histogram2d(start_year, awarded, bins=[year_bins, award_bins])
# H needs to be rotated and flipped
H = np.rot90(H)
H = np.flipud(H)
# Mask zeros
Hmasked = np.ma.masked_where(H==0,H) # Mask pixels with a value of zero
# Plot 2D histogram using pcolor
fig2 = plt.figure(figsize=(12,5))
plt.pcolormesh(xedges,yedges,Hmasked)
plt.plot(year_per_bin, awarded_mean_per_bin, 'r', label='mean', linewidth=2)
plt.plot(year_per_bin, awarded_median_per_bin, 'm', label='median',linewidth=3)
plt.xlabel('Start Year for each grant')
plt.ylabel('$ awarded to date')
plt.xlim(1970, 2015)
plt.ylim(0, max_award)
plt.legend(loc='upper left')
cbar = plt.colorbar()
cbar.ax.set_ylabel('# of grants')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment