Part 2: Visualize Twitch Data with Matplotlib

Part 1: Analyze Twitch Data with SQL

In this project, we'll use the results from the sql analysis in part 1 to visualize data from Twitch with matplotlib. This notebook must executed from the same kernel as and following sql.ipynb for all variables and imports to be defined (probably easiest to do with JupyterLab).

Additional import

In [17]:
from matplotlib import pyplot as plt

Pull data from sql results in part 1:

In [18]:
popular_games = popular_games.DataFrame() # sqlalchemy result_set to pandas dataframe
games = popular_games['game']
views =  popular_games['views']

Plot and style the graph:

In [19]:
ax = plt.subplot()
ax.set_xticks(range(10))
ax.set_xticklabels(games, rotation=90)
plt.title('Views for most popular games on Twitch')
plt.xlabel('Games')
plt.ylabel('Views')
plt.bar(range(len(games)),views, color='slateblue')
plt.show()

Create a pie chart visualizing location of League of Legends viewers

Pull data from sql results in Part 1:

In [20]:
lol_view_locs = lol_view_locs.DataFrame() # sqlalchemy result_set to pandas dataframe
countries = lol_view_locs['country']
views = lol_view_locs['lol_views']

Plot and style the graph:

In [21]:
ax = plt.subplot()
ax.axis('equal')
plt.title('League of Legends View Locations by Country')

# grab a color palette from colorbrewer
colors=['#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9','#bc80bd']
explode=[.1,0,0,0,0,0,0,0,0,0]
plt.pie(views,
        explode=explode,
        colors=colors,
        shadow=True,
        startangle=220,
        autopct='%1.0f%%',
        pctdistance=1.15
       )
plt.legend(countries, loc='right')
plt.show()

Create a line graph analyzing views by hour of day

Pull data from sql results in Part 1:

In [22]:
views_by_hour = views_by_hour.DataFrame() # sqlalchemy result_set to pandas dataframe
hours = views_by_hour['hour']
views = views_by_hour['views']

Plot and style the graph:

In [24]:
plt.title('Time Series for views by hour')
plt.xlabel('Hour')
plt.ylabel('Views')

ax1=plt.subplot()
ax1.set_xticks(range(24))

plt.plot(hours, views)
plt.show()