pandas
and seaborn
ipywidgets
: a package for interactive mini-apps in the Jupyter Notebookplotly
: produces D3.js charts using Python, and can convert your Python charts as wellcufflinks
: binds pandas
to plotly
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
mx = pd.read_csv('http://personal.tcu.edu/kylewalker/mexico.csv')
mx.plot(kind = 'scatter', x = 'pri10', y = 'mus09')
%matplotlib notebook
, and your plot becomes interactive!%matplotlib notebook
mx.plot(kind = 'scatter', x = 'pri10', y = 'mus09')
ipywidgets
package allows you to build basic graphical user interfaces (GUIs) to explore your data in the Notebookinteract
function in ipywidgets
from ipywidgets import interact
def add_five(x):
print(x + 5)
interact(add_five, x = (1, 100))
cols = ['mus09', 'pri10', 'sec10', 'ter10', 'gdp08']
def make_plot(x, y, title):
sns.lmplot(data = mx, x = x, y = y)
plt.title(title)
interact(make_plot, x = cols, y = cols, title = "Enter a plot title!")
def get_accidents(street_name):
part1 = 'https://data.fortworthtexas.gov/resource/kr8h-9zxd.json?streetname='
api_call = part1 + street_name
return pd.read_json(api_call)
def get_street(street = ['BERRY', 'UNIVERSITY', 'ROSEDALE']):
df = get_accidents(street)
return df.head()
interact(get_street)
matplotlib
plots to Plotly charts!import plotly.offline as py
py.init_notebook_mode()
mx.plot(kind = 'scatter', x = 'pri10', y = 'mus09')
fig = plt.gcf() # "Get current figure"
py.iplot_mpl(fig)
seaborn
seaborn
plots - though the styling will not always carry oversns.distplot(mx.pri10)
f = plt.gcf()
py.iplot_mpl(f)
pandas
data frames!iplot
instead of plot
!import cufflinks as cf
cf.go_offline()
mx.iplot(kind = 'scatter', mode = 'markers', x = 'pri10',
y = 'mus09', text = 'name', colors = 'green',
xTitle = '% of workforce in primary sector',
yTitle = 'Out-migration rate to the United States')
mx[['pri10', 'gdp08', 'mus09']].scatter_matrix()
mx.iplot(kind = 'scatter3d', x = 'mus09', y = 'gdp08',
z = 'pri10', mode = 'markers', text = 'name')
iplot()
function to view in the Jupyter Notebookimport numpy as np
import plotly.graph_objs as go
arr = np.random.randn(10, 10)
data = [go.Surface(z = arr)]
py.iplot(data)
from pandas_datareader import wb
ind = "SP.DYN.TFRT.IN"
tfr = wb.download(country = "all", indicator = ind, start = 1960, end = 2013)
tfr.reset_index(inplace = True)
def tfr_plot(country1, country2, country3):
countries = [country1, country2, country3]
subset = tfr[tfr.country.isin(countries)]
subset_wide = subset.pivot(index = "year", columns = "country",
values = "SP.DYN.TFRT.IN")
plot_data = [
go.Scatter(x = subset_wide.index,
y = subset_wide[country],
name = country,
mode = "lines")
for country in countries
]
plot_layout = go.Layout(title = "Total fertility rate",
xaxis = dict(rangeslider = dict()),
yaxis = dict(title = "TFR"))
fig = dict(data = plot_data, layout = plot_layout)
py.iplot(fig)
country_list = list(tfr.country.unique())
interact(tfr_plot, country1 = country_list,
country2 = country_list, country3 = country_list)
myplot = mx.iplot(kind = 'scatter', mode = 'markers', x = 'pri10',
y = 'mus09', text = 'name', colors = 'green',
xTitle = '% of workforce in primary sector',
yTitle = 'Out-migration rate to the United States',
asFigure = True)
py.plot(myplot, filename = 'mexico_plot.html')