Skip to content

Instantly share code, notes, and snippets.

@nhoffman
Created October 28, 2017 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhoffman/959a7e64890deeed8d63b3f22fd1b428 to your computer and use it in GitHub Desktop.
Save nhoffman/959a7e64890deeed8d63b3f22fd1b428 to your computer and use it in GitHub Desktop.
Git commit graph
from scipy.interpolate import interp1d
import numpy as np
import shlex
import subprocess as sp
import prettyplotlib as ppl
from matplotlib import pyplot as plt
from matplotlib.ticker import FuncFormatter
import io
from pandas import read_csv
cmd = "git log --no-merges --date=short --pretty='format:%ad\t%H\t%aN'"
p = sp.Popen(shlex.split(cmd), stdout=sp.PIPE)
# p.wait() will likely just hang if the log is long enough because the
# stdout buffer will fill up
stdout, _ = p.communicate()
table = read_csv(io.StringIO(stdout.decode('utf-8')), sep='\t',
names=['date', 'hash', 'author'], index_col=0,
parse_dates=True).sort()
table = table.to_period(freq='W')
commits_per_period = table.hash.groupby(level=0).aggregate(len)
dates = [p.start_time.date() for p in commits_per_period.index]
ncommits = commits_per_period.values
fn = interp1d(range(len(dates)), ncommits, 'cubic', bounds_error=False)
fig, ax = plt.subplots(1)
fig.set_size_inches((8, 2))
x = np.linspace(0, len(dates), 1000)
ppl.fill_between(x, 0, fn(x))
ax.set_title('Number of commits to Astropy')
ax.set_xlim(0, len(dates))
ax.set_ylim(0, max(ncommits) + 0.1 * max(ncommits))
ax.xaxis.set_ticks(np.linspace(0, len(dates) - 1, 8)[1:-1])
def formatter(x, p):
if x >= len(dates):
return ''
return dates[int(x)].strftime('%b %Y')
formatter = FuncFormatter(formatter)
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.tick_bottom()
ax.yaxis.tick_left()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# There must be a btter way to do this...
plt.setp(plt.xticks()[1], rotation=30)
fig.savefig('graph.png', bbox_inches='tight')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment