<jupyter_start><jupyter_text>_Lambda School Data Science_ Make explanatory visualizationsTody we will reproduce this [example by FiveThirtyEight:](https://fivethirtyeight.com/features/al-gores-new-movie-exposes-the-big-flaw-in-online-movie-ratings/)<jupyter_code>from IPython.display import display, Image

url = 'https://fivethirtyeight.com/wp-content/uploads/2017/09/mehtahickey-inconvenient-0830-1.png'
example = Image(url=url, width=400)

display(example)<jupyter_output><empty_output><jupyter_text>Using this data: https://github.com/fivethirtyeight/data/tree/master/inconvenient-sequel Objectives- add emphasis and annotations to transform visualizations from exploratory to explanatory- remove clutter from visualizationsLinks- [Strong Titles Are The Biggest Bang for Your Buck](http://stephanieevergreen.com/strong-titles/)- [Remove to improve (the data-ink ratio)](https://www.darkhorseanalytics.com/blog/data-looks-better-naked)- [How to Generate FiveThirtyEight Graphs in Python](https://www.dataquest.io/blog/making-538-plots/)  Make prototypesThis  helps us understand the problem<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


plt.style.use('fivethirtyeight')

fake = pd.Series([38, 3, 2, 1, 2, 4, 6, 5, 5, 33], 
                 index=range(1,11))

fake.plot.bar(color='C1', width=0.9);
fake2 = pd.Series(
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     2, 2, 2, 
     3, 3, 3,
     4, 4,
     5, 5, 5,
     6, 6, 6, 6,
     7, 7, 7, 7, 7,
     8, 8, 8, 8,
     9, 9, 9, 9, 
     10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10])

fake2.value_counts().sort_index().plot.bar(color='C1', width=0.9);<jupyter_output><empty_output><jupyter_text>Annotate with text<jupyter_code>plt.style.use('fivethirtyeight')

fake = pd.Series([38, 3, 2, 1, 2, 4, 6, 5, 5, 33], 
                 index=range(1,11))

ax = fake.plot.bar(color='C1', width=0.9)

ax.tick_params(labelrotation=0)

ax.text(x=-1.2, y=45.8, s="'An Inconvenient Sequal: Truth to Power' is divisive", fontsize= 15, fontweight='bold');
ax.text(x=-0.9, y=43, s='IMBd ratings for the films as of Aug.29',fontsize=12);

ax.set(xlabel='Rating', ylabel='Percent of Total Votes',
       yticks=range(0,50,10));<jupyter_output><empty_output><jupyter_text>Reproduce with real data<jupyter_code>df = pd.read_csv('https://raw.githubusercontent.com/fivethirtyeight/data/master/inconvenient-sequel/ratings.csv')
df.shape
pd.options.display.max_columns = 500
df.head()
df.sample().T
df.timestamp = pd.to_datetime(df.timestamp)
df.timestamp.describe()
df = df.set_index('timestamp')
df.head()
df['2017-08-29']
df[df.category == 'IMDb users']
lastday = df['2017-08-29']
lastday[lastday.category == 'IMDb users'].tail()
final = df.tail(1)
columns = [str(i) + '_pct' for i in range(1,11)]
data = final[columns].T
data.plot.bar();
data.index = range(1,11)
ax = data.plot.bar(color='C1', width=0.9, legend=False)

ax.tick_params(labelrotation=0)

ax.text(x=-1.2, y=45.8, s="'An Inconvenient Sequal: Truth to Power' is divisive", fontsize= 15, fontweight='bold');
ax.text(x=-0.9, y=43, s='IMBd ratings for the films as of Aug.29',fontsize=12);

ax.set(xlabel='Rating', ylabel='Percent of Total Votes',
       yticks=range(0,50,10));<jupyter_output><empty_output><jupyter_text>ASSIGNMENTReplicate the lesson code. I recommend that you [do not copy-paste](https://docs.google.com/document/d/1ubOw9B3Hfip27hF2ZFnW3a3z9xAgrUDRReOEo-FHCVs/edit). STRETCH OPTIONS Reproduce another example from [FiveThityEight's shared data repository](https://data.fivethirtyeight.com/).If you aren't sure what to choose, try:- the chart titled ["Men dominated Al Gore's IMDb movie rating"](https://fivethirtyeight.com/features/al-gores-new-movie-exposes-the-big-flaw-in-online-movie-ratings/)- or the tutorial, [How to Generate FiveThirtyEight Graphs in Python](https://www.dataquest.io/blog/making-538-plots/)Other options include:- [thanksgiving-2015](https://fivethirtyeight.com/features/heres-what-your-part-of-america-eats-on-thanksgiving/) (try the [`altair`](https://altair-viz.github.io/gallery/index.htmlmaps) library)- [candy-power-ranking](https://fivethirtyeight.com/features/the-ultimate-halloween-candy-power-ranking/) (try the [`statsmodels`](https://www.statsmodels.org/stable/index.html) library)- or another example of your choice! Make more charts!Choose a chart you want to make, from [FT's Visual Vocabulary poster](http://ft.com/vocabulary).Find the chart in an example gallery of a Python data visualization library:- [Seaborn](http://seaborn.pydata.org/examples/index.html)- [Altair](https://altair-viz.github.io/gallery/index.html)- [Matplotlib](https://matplotlib.org/gallery.html)- [Pandas](https://pandas.pydata.org/pandas-docs/stable/visualization.html)Reproduce the chart. [Optionally, try the "Ben Franklin Method."](https://docs.google.com/document/d/1ubOw9B3Hfip27hF2ZFnW3a3z9xAgrUDRReOEo-FHCVs/edit) If you want, experiment and make changes.Take notes. Consider sharing your work with your cohort!<jupyter_code>candy = pd.read_csv('https://raw.githubusercontent.com/fivethirtyeight/data/master/candy-power-ranking/candy-data.csv')
candy.shape
candy.head()
candy.winpercent = candy.winpercent / 100
candy['chocolate_and_caramel'] = candy.chocolate + candy.caramel
candy[candy.chocolate == 1].winpercent.mean()
candy[candy.caramel == 1].winpercent.mean()
candy.pivot_table(index='chocolate_and_caramel', values='winpercent')
mappings = {0:0, 1:0, 2:1}
candy.chocolate_and_caramel = candy.chocolate_and_caramel.map(mappings)
candy.pivot_table(index='chocolate_and_caramel', values='winpercent')
# i want to create my own little deck of cards library to help get an intuition for OOP

import random
class Card(object):
    def __init__(self, value, suit):
        self.value = value
        self.suit = suit
    def show(self):
        print('{} of {}'.format(self.value,self.suit))

class Deck(object):
    def __init__(self):
        self.cards = []
        self.build()
    
    def build(self):
        for s in ['spades','clubs','diamonds','hearts']:
            [self.cards.append(Card(v,s)) for v in range(1,14)]
    
    def show(self):
        [c.show() for c in self.cards]
    
    def shuffle(self):
        for i in range(len(self.cards)-1, 0, -1):
            r = random.randint(0,i)
            self.cards[i], self.cards[r] = self.cards[r], self.cards[i]
    
    def drawCard(self):
        return self.cards.pop()

class Player(object):

    def __init__(self, name):
        self.name = name
        self.hand = []
    
    def draw(self, deck):
        self.hand.append(deck.drawCard())
        return self   # return allows us to chain these draw methods.
    
    def showHand(self):
        [c.show() for c in self.hand]

deck = Deck()
mac = Player('mac')
deck.shuffle()
mac.draw(deck)
mac.draw(deck).draw(deck)
mac.showHand()
# could now make a blackjack game or something but this gave me a better intuition<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup