Skip to content

Instantly share code, notes, and snippets.

@ixtel
Forked from bbengfort/report.py
Created October 19, 2015 12:58
Show Gist options
  • Save ixtel/effddf109bf9ee2ed28f to your computer and use it in GitHub Desktop.
Save ixtel/effddf109bf9ee2ed28f to your computer and use it in GitHub Desktop.
Plotting ingestion from CSVs
#!/usr/bin/env python
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
PATH = os.path.abspath(os.path.dirname(__file__))
def readcsv(path):
converters = { 1: mdates.strpdate2num('%Y-%m-%d')}
return np.loadtxt(path, delimiter=",", unpack=True, converters=converters)
def readcsvs(dir):
for path in os.listdir(dir):
name, ext = os.path.splitext(path)
if ext == '.csv':
name = " ".join(name.split("_")).title()
path = os.path.join(dir, path)
yield name, readcsv(path)
def plot_line(dir):
colors = {
'deactivated': 'r-',
'new': 'g-',
'updated': 'b-'
}
# Set up the figure
plt.figure(1)
# Set up products subplot
prodplt = plt.subplot(211)
plt.title("Products Ingested")
plt.ylabel("# Products")
plt.grid(True)
# Set up skus subplot
skusplt = plt.subplot(212)
plt.title("SKUs Ingested")
plt.ylabel("# SKUs")
plt.grid(True)
for idx, (name, data) in enumerate(readcsvs(dir)):
counts, dates = data
parts = [x.lower() for x in name.split()]
color = colors[parts[0]]
if parts[1] == "products":
lines = prodplt.plot_date(x=dates, y=counts, fmt=color, label=parts[0])
else:
lines = skusplt.plot_date(x=dates, y=counts, fmt=color, label=parts[0])
prodplt.legend()
skusplt.legend()
plt.show()
if __name__ == '__main__':
plot_line(PATH)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment