''' Created on Jan 14, 2015 @author: Brett Paufler (c) Copyright Brett Paufler ''' from reddit_offline import quick_report from reddit_offline import thousands_to_dataframe from reddit_offline import tally_column_groups import pandas as pd import matplotlib.pyplot as plt def subreddit_graph(cat="new", chart="pie", hour=24): '''makes pie and bar charts showing composition of front page also html table (probably not going to use) with quantified number of hits cat = "new" or "front_page" ''' #Data Reduced to Subreddit dF = thousands_to_dataframe(hour_filter=hour) dF = dF[dF["category"]==cat] dF = tally_column_groups(dF, "subreddit") dF = pd.DataFrame(dF, columns=["count","subreddit"]) dF = dF.set_index("subreddit") quick_report(dF) #Chart Output s = 5 dF.plot(kind=chart, figsize=(s,s), legend=False, title=cat.upper(), subplots=True, fontsize=5) plt.xlabel=("") plt.ylabel("") sN = "subreddits_%s_%s_%d.png" % (cat,chart,hour) plt.savefig(sN) #HTML OUTPUT html = "\n\n" html += dF.to_html() html += "\n\n" sN = "subreddits_%s_%s_%d.html" % (cat,chart,hour) with open(sN, "w") as f: f.write(html) print html return dF def plot_all(): for h in [24]: for cat in ["new","front_page"]: for chart in ["pie","bar"]: subreddit_graph(cat,chart,h) def new_to_front_conversion_html(hour=24): dF = thousands_to_dataframe(hour_filter=hour) nF = dF[dF["category"]=="new"] nF = tally_column_groups(nF, "subreddit") nF = pd.DataFrame(nF, columns=["New","subreddit"]) nF = nF.set_index("subreddit") quick_report(nF) pF = dF[dF["category"]=="front_page"] pF = tally_column_groups(pF, "subreddit") pF = pd.DataFrame(pF, columns=["Front Page","subreddit"]) pF = pF.set_index("subreddit") quick_report(pF) dF = pd.merge(nF,pF, left_index=True, right_index=True) dF["Conversion"] = dF["Front Page"]/dF["New"] quick_report(dF) print dF s = 5 dF.plot(kind="bar", figsize=(s,s), legend=False, title="Front Page vs New", fontsize=5) #subplots=True plt.xlabel=("") plt.ylabel("") sN = "subreddits_comboChart.png" plt.savefig(sN) html = "\n\n" html += dF.to_html()#float_format=(lambda x: '%d%%' % int(100*x))) html += "\n\n" sN = "subreddits_comboChart.html" with open(sN, "w") as f: f.write(html) #print html plot_all() new_to_front_conversion_html() print "ALL DONE"