<jupyter_start><jupyter_text>Advanced VisualizationA couple of different visualization libraries in this notebook. Starting w/ `altair`, running through some `matplotlib`, hitting `bqplot`, and ending with `ipyvolume`.  Altair<jupyter_code># uncomment the line below and run the cell, then run the cell again to get altair tooltip.
# %load ../scripts/altair-example.py
%matplotlib inline
import matplotlib.pyplot as plt

import seaborn as sns
sns.set_context('poster')
sns.set_style('whitegrid') 
# sns.set_style('darkgrid') 
plt.rcParams['figure.figsize'] = 12, 8  # plotsize 

import numpy as np
import pandas as pd
from pandas.tools.plotting import scatter_matrix
from sklearn.datasets import load_boston

import warnings
warnings.filterwarnings('ignore')

import matplotlib as mpl<jupyter_output><empty_output><jupyter_text>NoteUsing cleaned data from [Data Cleaning](Data%20Cleaning.ipynb) Notebook. See Notebook for details.<jupyter_code>df = pd.read_csv("../data/coal_prod_cleaned.csv")
df.head()
colors = sns.color_palette(n_colors=df.Year.nunique())
color_dict = dict(zip(sorted(df.Year.unique()), colors))
color_dict
for year in sorted(df.Year.unique()[[0, 2, -1]]):
    plt.scatter(df[df.Year == year].Labor_Hours,
                df[df.Year == year].Production_short_tons, 
                c=color_dict[year],
                s=50,
                label=year,
               )
plt.xlabel("Total Hours Worked")
plt.ylabel("Total Amount Produced")
plt.legend()
plt.savefig("../ex1.png")
plt.style.available
mpl.style.use('seaborn-colorblind')
for year in sorted(df.Year.unique()[[0, 2, -1]]):
    plt.scatter(df[df.Year == year].Labor_Hours,
                df[df.Year == year].Production_short_tons, 
#                 c=color_dict[year],
                s=50,
                label=year,
               )
plt.xlabel("Total Hours Worked")
plt.ylabel("Total Amount Produced")
plt.legend();
# plt.savefig("ex1.png")
df_dict = load_boston()
features = pd.DataFrame(data=df_dict.data, columns = df_dict.feature_names)
target = pd.DataFrame(data=df_dict.target, columns = ['MEDV'])
df = pd.concat([features, target], axis=1)
df['Zone'] = df['ZN'].astype('category')
df.head()
# Target variable
fig, ax = plt.subplots(figsize=(6, 4))
sns.distplot(df.MEDV, ax=ax, rug=True, hist=False);
fig, ax = plt.subplots(figsize=(10,7))
sns.kdeplot(df.LSTAT,
            df.MEDV,
            ax=ax);
fig, ax = plt.subplots(figsize=(10, 10))
scatter_matrix(df[['MEDV', 'LSTAT', 'CRIM', 'RM', 'NOX', 'DIS']], alpha=0.2, diagonal='hist', ax=ax);
# fig, ax = plt.subplots(figsize=(10, 10))
sns.pairplot(data=df, 
             vars=['MEDV', 'LSTAT', 'CRIM', 'RM', 'NOX', 'DIS'],
             plot_kws={'s':20, 'alpha':0.5},
            );<jupyter_output><empty_output><jupyter_text>Quantile cuts<jupyter_code>players = pd.read_csv("../data/raw_players.csv.gz", compression='gzip')
players.head()
weight_categories = ["vlow_weight",
                     "low_weight",
                     "mid_weight",
                     "high_weight",
                     "vhigh_weight",
                    ]

players['weightclass'] = pd.qcut(players['weight'],
                                 len(weight_categories),
                                 weight_categories)
players.head()<jupyter_output><empty_output><jupyter_text>BQPlotExamples here are shamelessly stolen from the amazing: https://github.com/maartenbreddels/jupytercon-2017/blob/master/jupytercon2017-widgets.ipynb<jupyter_code>from IPython.display import YouTubeVideo
YouTubeVideo("uHPcshgTotE", width=560, height=315)
# mixed feelings about this import
import bqplot.pyplot as plt
import numpy as np
x = np.linspace(0, 2, 50)
y = x**2
fig = plt.figure()
scatter = plt.scatter(x, y)
plt.show()
fig.animation_duration = 500
scatter.y = x**.5 + 2
scatter.selected_style = {'stroke':'red', 'fill': 'orange'}
plt.brush_selector();
scatter.selected
scatter.selected = [1,2,10,40]<jupyter_output><empty_output><jupyter_text>ipyvolume<jupyter_code>import ipyvolume as ipv
N = 1000
x, y, z = np.random.random((3, N))
fig = ipv.figure()
scatter = ipv.scatter(x, y, z, marker='box')
ipv.show()
scatter.x = scatter.x + 0.5
scatter.x = x - .25
scatter.color = "green"
scatter.size = 5
scatter.color = np.random.random((N,3))
scatter.size = 2
ex = ipv.datasets.animated_stream.fetch().data
ex.shape
ex[:, ::, ::4].shape
ipv.figure()
ipv.style.use('dark')
quiver = ipv.quiver(*ipv.datasets.animated_stream.fetch().data[:,::,::4], size=5)
ipv.animation_control(quiver, interval=200)
ipv.show()
ipv.style.use('light')
quiver.geo = "cat"
N = 1000*1000
x, y, z = np.random.random((3, N)).astype('f4')
ipv.figure()
s = ipv.scatter(x, y, z, size=0.2)
ipv.show()
ipv.save("3d-example-plot.html")
# I don't know why this doesn't work. It may *just work* for you.
!open 3d-example-plot.html<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1578186325139434, 'fraction_numerical': 0.020450320181780623, 'mean_word_length': 3.8517034068136273, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '168464', 'license': 'MIT', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1748, 'n_words': 350, 'path': 'notebooks/06-Advanced-Visualization.ipynb', 'repo_name': 'nknezek/jupyter_tips_and_tricks', 'size': '4841'}