labours.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import argparse
  2. from datetime import datetime, timedelta
  3. import sys
  4. import numpy
  5. if sys.version_info[0] < 3:
  6. # OK, ancients, I will support Python 2, but you owe me a beer
  7. input = raw_input
  8. def parse_args():
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument("--output", default="",
  11. help="Path to the output file (empty for display).")
  12. parser.add_argument("--text-size", default=12,
  13. help="Size of the labels and legend.")
  14. parser.add_argument("--backend", help="Matplotlib backend to use.")
  15. args = parser.parse_args()
  16. return args
  17. def main():
  18. args = parse_args()
  19. import matplotlib
  20. if args.backend:
  21. matplotlib.use(args.backend)
  22. import matplotlib.pyplot as pyplot
  23. import pandas
  24. import seaborn # to get nice colors, he-he
  25. start, granularity, sampling = input().split()
  26. start = datetime.fromtimestamp(int(start))
  27. granularity = int(granularity)
  28. sampling = int(sampling)
  29. matrix = numpy.array([numpy.fromstring(line, dtype=int, sep=" ")
  30. for line in sys.stdin.read().split("\n")[:-1]]).T
  31. pyplot.stackplot(
  32. pandas.date_range(start, periods=matrix.shape[1], freq="%dD" % sampling),
  33. matrix,
  34. labels=["%s - %s" % ((start + timedelta(days=i * granularity)).date(),
  35. (start + timedelta(days=(i + 1) * granularity)).date())
  36. for i in range(matrix.shape[0])])
  37. pyplot.legend(loc=2, fontsize=args.text_size)
  38. pyplot.ylabel("Lines of code", fontsize=args.text_size)
  39. pyplot.tick_params(labelsize=args.text_size)
  40. pyplot.gcf().set_size_inches(12, 9)
  41. if not args.output:
  42. pyplot.gcf().canvas.set_window_title(
  43. "Hercules %d x %d (granularity %d, sampling %d)" %
  44. (matrix.shape + (granularity, sampling)))
  45. pyplot.show()
  46. else:
  47. pyplot.tight_layout()
  48. pyplot.savefig(args.output)
  49. if __name__ == "__main__":
  50. sys.exit(main())