In [ ]:
In [ ]:
In [2]:
# %load ../scripts/altair-example.py
#!/usr/bin/env python
"""
Altair Tooltip Example
======================
An example script that Jonathan lightly modified to make it work in a Notebook.
"""
import sys
import altair as alt
alt.renderers.enable("notebook")
from vega_datasets import data
iris = data.iris()
# alt.renderers.enable('default')
alt.Chart(iris).mark_point().encode(x="petalLength", y="petalWidth", color="species")
cars = data.cars()
LITERS_PER_GALLON = 3.78541
cars["Miles_per_Liter"] = cars["Miles_per_Gallon"] * LITERS_PER_GALLON
alt.Chart(cars).mark_circle(size=60).encode(
x="Horsepower",
y="Miles_per_Gallon",
color="Origin",
tooltip=["Name", "Origin", "Horsepower", "Miles_per_Gallon"],
).interactive()
Out[2]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [3]:
%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.plotting import scatter_matrix
from sklearn.datasets import load_boston
import warnings
warnings.filterwarnings('ignore')
import matplotlib as mpl
Using cleaned data from Data Cleaning Notebook. See Notebook for details.
In [4]:
df = pd.read_csv("../data/coal_prod_cleaned.csv")
In [5]:
df.head()
Out[5]:
In [6]:
colors = sns.color_palette(n_colors=df.Year.nunique())
In [7]:
color_dict = dict(zip(sorted(df.Year.unique()), colors))
color_dict
Out[7]:
In [8]:
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=np.c_[color_dict[year]],
s=500,
label=year,
)
plt.xlabel("Total Hours Worked")
plt.ylabel("Total Amount Produced")
plt.legend()
plt.savefig("../ex1.png")
In [9]:
plt.style.available
Out[9]:
In [10]:
mpl.style.use('seaborn-colorblind')
In [ ]:
In [11]:
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")
In [12]:
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)
In [13]:
df['Zone'] = df['ZN'].astype('category')
df.head()
Out[13]:
In [14]:
# Target variable
fig, ax = plt.subplots(figsize=(6, 4))
sns.distplot(df.MEDV, ax=ax, rug=True, hist=False);
In [15]:
fig, ax = plt.subplots(figsize=(10,7))
sns.kdeplot(df.LSTAT,
df.MEDV,
ax=ax);
In [16]:
fig, ax = plt.subplots(figsize=(10, 10))
scatter_matrix(df[['MEDV', 'LSTAT', 'CRIM', 'RM', 'NOX', 'DIS']], alpha=0.2, diagonal='hist', ax=ax);
In [17]:
# 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},
);
In [18]:
players = pd.read_csv("../data/raw_players.csv.gz", compression='gzip')
In [19]:
players.head()
Out[19]:
In [20]:
weight_categories = ["vlow_weight",
"low_weight",
"mid_weight",
"high_weight",
"vhigh_weight",
]
players['weightclass'] = pd.qcut(players['weight'],
len(weight_categories),
weight_categories)
In [21]:
players.head()
Out[21]:
Examples here are shamelessly stolen from the amazing: https://github.com/maartenbreddels/jupytercon-2017/blob/master/jupytercon2017-widgets.ipynb
In [22]:
from IPython.display import YouTubeVideo
In [22]:
YouTubeVideo("uHPcshgTotE", width=560, height=315)
Out[22]:
In [23]:
# mixed feelings about this import
import bqplot.pyplot as plt
import numpy as np
In [24]:
x = np.linspace(0, 2, 50)
y = x**2
In [25]:
fig = plt.figure()
scatter = plt.scatter(x, y)
plt.show()
In [27]:
fig.animation_duration = 500
scatter.y = x**.5
In [28]:
scatter.selected_style = {'stroke':'red', 'fill': 'orange'}
plt.brush_selector();
In [30]:
scatter.selected
Out[30]:
In [34]:
scatter.selected = [1,2,10,42,45]
In [35]:
import ipyvolume as ipv
In [ ]:
In [36]:
N = 1000
x, y, z = np.random.random((3, N))
In [37]:
fig = ipv.figure()
scatter = ipv.scatter(x, y, z, marker='box')
ipv.show()
In [54]:
scatter.x = x + np.random.rand() - 0.5
In [58]:
scatter.color = "green"
scatter.size = 5
In [56]:
scatter.color = np.random.random((N,3))
In [57]:
scatter.size = 2
In [59]:
ex = ipv.datasets.animated_stream.fetch().data
In [60]:
ex.shape
Out[60]:
In [61]:
ex[:, ::, ::4].shape
Out[61]:
In [62]:
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')
In [63]:
quiver.geo = "cat"
In [68]:
# N = 1000*1000
N = 1000
x, y, z = np.random.random((3, N)).astype('f4')
In [69]:
ipv.figure()
s = ipv.scatter(x, y, z, size=0.2, )
ipv.show()
In [70]:
ipv.save("3d-example-plot.html")
In [71]:
!open 3d-example-plot.html
In [72]:
name = "Billy"
In [ ]: