In this notebook I use Matplotlib.pyplot to create simple yet powerful visualizations of PyBites Twitter activity, our blog’s tag usage and our website traffic. Lastly I use Google trends to see popularity of various social media.
In [1]:
from collections import Counter, OrderedDict
import csv
import datetime
import itertools
import operator
import random
import re
import feedparser
from matplotlib import pyplot as plt
import numpy as np
import requests
# to show plots inside our Jupyter notebook
%matplotlib inline
1. A simple line chart: how often do we tweet per day?¶
For this I ran our challenge 04 code to get a csv with last 200 tweets.
In [2]:
BASE_URL = 'http://projects.bobbelderbos.com/matplotlib/'
def get_csv(url):
with requests.Session() as s:
download = s.get(url)
decoded_content = download.content.decode('utf-8')
cr = csv.reader(decoded_content.splitlines(), delimiter=',')
return list(cr)[1:]
def get_dates(csv):
for row in reversed(csv):
yield row[1].split()[0]
In [3]:
pybites_csv = get_csv('{}pybites.csv'.format(BASE_URL))
c = Counter(list(get_dates(pybites_csv)))
c_sorted = sorted(c.items(), key=operator.itemgetter(0))
labels, values = zip(*c_sorted)
indexes = np.arange(len(labels))
width = 1
plt.plot(indexes, values)
#plt.bar(indexes, values, width)
plt.xticks(indexes + width * 0.5, labels, rotation=90)
fig = plt.gcf()
fig.set_size_inches(15,15)
plt.title('PyBites Twitter activity (last 200 tweets)')
plt.show() # plt.savefig('file.png') to save to file