<jupyter_start><jupyter_text>Solutions About the DataIn this notebook, we will be working with 2 datasets:- Facebook's stock price throughout 2018 (obtained using the [`stock_analysis` package](https://github.com/stefmolin/stock-analysis))- Earthquake data from September 18, 2018 - October 13, 2018 (obtained from the US Geological Survey (USGS) using the [USGS API](https://earthquake.usgs.gov/fdsnws/event/1/)) Setup<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

fb = pd.read_csv('../../ch_06/data/fb_stock_prices_2018.csv', index_col='date', parse_dates=True)
quakes = pd.read_csv('../../ch_06/data/earthquakes.csv')<jupyter_output><empty_output><jupyter_text>Exercise 1Using `seaborn`, create a heatmap to visualize the correlation coefficients between earthquake magnitude and whether or not there is a tsunami with the 'mb' `magType`.<jupyter_code>sns.heatmap(
    quakes.query('magType == "mb"')[['mag','tsunami']].corr(), 
    annot=True, center=0
)
plt.suptitle('Correlation between earthquake magnitude and whether\nthere was a tsunami with `mb` magType')<jupyter_output><empty_output><jupyter_text>Exercise 2Create a box plot of Facebook volume traded and closing price and draw reference lines for the bounds of a Tukey fence with a multiplier of 1.5.<jupyter_code>cols = ['volume', 'close']
subset = fb[cols]

quantiles = subset.quantile([0.25, 0.75])
quantiles.loc['iqr',:] = quantiles.loc[0.75,:] - quantiles.loc[0.25,:]

axes = subset.plot(kind='box', subplots=True, figsize=(8, 3), title=['Volume Traded', 'Closing Price'])
for ax, col in zip(axes, cols):
    stats = quantiles[col]
    lower = stats.loc[0.25] - 1.5 * stats['iqr']
    upper = stats.loc[0.75] + 1.5 * stats['iqr']
    for bound, name in zip([lower, upper], ['lower', 'upper']):
        ax.axhline(
            bound, 
            color='red', 
            linestyle='dashed',
            label=name
        )
        ax.legend()
plt.suptitle('Facebook volume traded and closing price with Tukey Fence bounds', y=1.1)<jupyter_output><empty_output><jupyter_text>Exercise 3Fill in the area inside the bounds in the plot from exercise 2.<jupyter_code>cols = ['volume', 'close']
subset = fb[cols]

quantiles = subset.quantile([0.25, 0.75])
quantiles.loc['iqr',:] = quantiles.loc[0.75,:] - quantiles.loc[0.25,:]

axes = subset.plot(kind='box', subplots=True, figsize=(8, 3), title=['Volume Traded', 'Closing Price'])
for ax, col in zip(axes, cols):
    stats = quantiles[col]
    lower = stats.loc[0.25] - 1.5 * stats['iqr']
    upper = stats.loc[0.75] + 1.5 * stats['iqr']
    ax.fill_between(
        [0,2],
        lower,
        upper, 
        color='#ff6666'
    )
plt.suptitle('Facebook volume traded and closing price with shaded Tukey Fence', y=1.1)<jupyter_output><empty_output><jupyter_text>Exercise 4Use `axvspan()` to shade a rectangle from '2018-07-25' to '2018-07-31' marking the large decline in Facebook price on a line graph of closing price.<jupyter_code>fb.close.plot(kind='line', figsize=(5, 3))
plt.axvspan('2018-07-25', '2018-07-31', alpha=0.2, label='July decline')
plt.ylabel('Price($)')
plt.suptitle('Facebook Closing Price')
plt.legend()<jupyter_output><empty_output><jupyter_text>Exercise 5Using the Facebook stock data, annotate the following three events on a line graph of closing price:1. 'Disappointing user growth announced after close' on July 25, 20182. 'Cambridge Analytica story breaks' on March 19, 2018 (when it affected the market)3. 'FTC launches investigation' on March 20, 2018<jupyter_code>fb.close.plot(kind='line', figsize=(5, 3))
notes = [
    ('Disappointing user growth announced after close', '2018-07-25'),
    ('Cambridge Analytica story breaks', '2018-03-19'),
    ('FTC launches investigation', '2018-03-20')
]
for text, date in notes:
    y_value = fb.close[date]
    jitter = np.random.uniform(-20, -10, 1)
    plt.annotate(
        text, 
        xy=(date, y_value),
        xytext=('2019-02-25', y_value + jitter),
        arrowprops=dict(arrowstyle='->')
    )
plt.ylabel('Price($)')
plt.suptitle('Facebook Closing Price')<jupyter_output><empty_output><jupyter_text>Exercise 6Modify the `reg_resid_plot()` function to use a matplotlib colormap instead of cycling between two colors. Remember, for this use case, you should pick a qualitative colormap or make your own.<jupyter_code>import itertools

import matplotlib.pyplot as plt
from matplotlib import cm
import seaborn as sns

def reg_resid_plots(data):
    """
    Using seaborn, plot the regression and residuals
    plots side-by-side for every permutation of 2 columns 
    in the data.

    Parameters:
        - data: A pandas DataFrame
    Returns:
        A matplotlib Figure object.
    """
    num_cols = data.shape[1]
    permutation_count = num_cols * (num_cols - 1)

    fig, ax = plt.subplots(
        permutation_count, 
        2, 
        figsize=(15, 4 * permutation_count)
    )

    for (x, y), axes, color in zip(
        itertools.permutations(data.columns, 2), 
        ax,
        [cm.Dark2(i) for i in range(len(ax))]
    ):
        for subplot, func in zip(axes, (sns.regplot, sns.residplot)):
            func(
                x=x, 
                y=y,
                data=data,
                ax=subplot,
                color=color
            )
    plt.close()
    return fig
reg_resid_plots(fb[['close', 'volume']])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13151137197358767, 'fraction_numerical': 0.036500366837857665, 'mean_word_length': 3.688736027515047, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '356060', 'license': 'MIT', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1785, 'n_words': 504, 'path': 'solutions/ch_06/solutions.ipynb', 'repo_name': 'rvmuthukumar/Hands-On-Data-Analysis-with-Pandas', 'size': '5452'}