Created
July 15, 2014 17:09
-
-
Save bbengfort/73056d399756c1cc8f0c to your computer and use it in GitHub Desktop.
Plotting ingestion from CSVs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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