import pandas as pd
import pylab as plt
import param
import datetime as dt
import panel as pn
def interact_example(a=2, b=3):
plot = plt.figure()
ax = plot.add_subplot(111)
pd.Series({'a':a, 'b':b}).plot(kind='bar',ax=ax)
plt.tight_layout()
plt.close(plot)
return plot
pn.interact(interact_example)
WARNING:param.Column00005: Displaying Panel objects in the notebook requires the panel extension to be loaded. Ensure you run pn.extension() before displaying objects in the notebook.
Column [0] Column [0] IntSlider(end=6, name='a', start=-2, value=2) [1] IntSlider(end=9, name='b', start=-3, value=3) [1] Row [0] Matplotlib(Figure, name='interactive00004')
import sqlite3
con = sqlite3.connect('../Chapter16/data/311.db')
Q = '''
SELECT date(created_date) as date, lower(borough) as boro, complaint_type, COUNT(*) as complaints
FROM raw WHERE borough != 'Unspecified' GROUP BY 1,2,3;
'''
DATA = pd.read_sql_query(Q, con)
DATA.head(3)
date | boro | complaint_type | complaints | |
---|---|---|---|---|
0 | 2019-01-01 | bronx | APPLIANCE | 5 |
1 | 2019-01-01 | bronx | Air Quality | 1 |
2 | 2019-01-01 | bronx | Blocked Driveway | 98 |
DATA['date'] = pd.to_datetime(DATA['date'])
DATA['date'].describe()
count 86976 unique 205 top 2019-05-21 00:00:00 freq 510 first 2019-01-01 00:00:00 last 2019-07-24 00:00:00 Name: date, dtype: object
DATA.boro.unique()
array(['bronx', 'brooklyn', 'manhattan', 'queens', 'staten island'], dtype=object)
DATA['date'].max()
Timestamp('2019-07-24 00:00:00')
boro_total = DATA.groupby(['date', 'boro'])['complaints'].sum().unstack()
print(boro_total.head(5).to_string())
boro bronx brooklyn manhattan queens staten island date 2019-01-01 995 1657 859 1237 249 2019-01-02 1675 2444 1307 1880 649 2019-01-03 1450 2532 1420 1799 484 2019-01-04 1472 2407 1417 1835 425 2019-01-05 1085 1551 954 1250 292
# boro_total
bounds = (dt.datetime(2019,1,1), dt.datetime(2019,6,14))
class Timeline(param.Parameterized):
dr = param.DateRange(bounds=bounds, default=bounds)
boros = param.ListSelector(default=['Manhattan', 'Bronx', 'Brooklyn', 'Queens', 'Staten Island'], objects=['Manhattan', 'Bronx', 'Brooklyn', 'Queens', 'Staten Island'])
@param.depends('dr', 'boros')
def view_tl(self):
start, end = pd.to_datetime(self.dr[0]), pd.to_datetime(self.dr[1])
tl_data = boro_total.loc[(boro_total.index >= start) & (boro_total.index <= end), [el.lower() for el in self.boros]]
plot = plt.figure(figsize=(10,5))
ax = plot.add_subplot(111)
tl_data.plot(ax=ax, linewidth=1)
ax.legend(loc=4)
plt.tight_layout()
plt.close(plot)
return plot
@param.depends('dr', 'boros')
def view_top(self, N=5):
start, end = pd.to_datetime(self.dr[0]), pd.to_datetime(self.dr[1])
boro_mask = DATA.boro.isin([el.lower() for el in self.boros])
time_mask = (DATA.date >= start) & (DATA.date <= end)
top = DATA[boro_mask & time_mask]
S = top.groupby(['complaint_type', 'boro'])['complaint_type'].count().unstack()
topN = S.iloc[S.sum(1).argsort()].tail(N)
plot = plt.figure()
ax = plot.add_subplot(111)
topN.plot(kind='barh',stacked=True, ax=ax)
plt.tight_layout()
plt.close(plot)
return plot
pn.extension()
T = Timeline()
panel = pn.Column( '<h1>NYC 311 dashboard</h1>',
T.view_tl,
pn.Row(T.param, T.view_top), sizing_mode='stretch_width')
panel.show()
<bokeh.server.server.Server at 0x1533172e8>