|
import panel as pn |
|
import vega_datasets |
|
|
|
|
|
pn.extension(design='bootstrap') |
|
pn.extension('vega') |
|
template = pn.template.BootstrapTemplate( |
|
title='Nan-Hsin Lin', |
|
) |
|
|
|
|
|
|
|
def create_plot(subgroup, date_range, moving_av_window): |
|
|
|
|
|
df3 = df2[df2['choice'] == 'approve'].copy() |
|
df3 = df3[df3['subgroup'] == subgroup] |
|
df3['smoothed_rate'] = df3['rate'].rolling(moving_av_window).mean().shift(-int(moving_av_window/2)) |
|
start_date, end_date = date_range |
|
df3 = df3[(df3['timestamp'] >= np.datetime64(start_date)) & (df3['timestamp'] <= np.datetime64(end_date))] |
|
|
|
|
|
rate_line = alt.Chart(df3).mark_line(strokeWidth=2, color='red').encode( |
|
x=alt.X('timestamp:T', axis=alt.Axis(title=None)), |
|
y=alt.Y('average(smoothed_rate):Q', axis=alt.Axis(title='move_avg'), scale=alt.Scale(domain=[30, 60])) |
|
) |
|
|
|
|
|
rate_scatter = alt.Chart(df3).mark_point(color='grey', size=2, opacity=0.7).encode( |
|
x=alt.X('timestamp:T', axis=alt.Axis(title=None)), |
|
y=alt.Y('average(rate):Q', axis=alt.Axis(title='approve'), scale=alt.Scale(domain=[30, 60])), |
|
) |
|
|
|
|
|
plot = alt.layer(rate_line, rate_scatter).configure_view(strokeWidth=0) |
|
|
|
|
|
return plot |
|
|
|
|
|
select = pn.widgets.Select(name='Select', options=df2['subgroup'].unique().tolist()) |
|
|
|
|
|
dateSlider = pn.widgets.DateRangeSlider(name='Date Range Slider', |
|
start=df2['timestamp'].min(), |
|
end=df2['timestamp'].max(), |
|
value=(df2['timestamp'].min(), df2['timestamp'].max())) |
|
|
|
|
|
avgSlider = pn.widgets.IntSlider(name='Moving average window', start=1, end=100, value=1) |
|
|
|
|
|
plot_widgets = pn.Row(pn.bind(create_plot, select, dateSlider, avgSlider)) |
|
|
|
|
|
maincol = pn.Column() |
|
maincol.append("# SI649 Lab07") |
|
maincol.append("Hello! This is **Nan**. I love information visualization! Email me: [[email protected]](mailto:[email protected])") |
|
maincol.append(plot_widgets) |
|
maincol.append(select) |
|
maincol.append(dateSlider) |
|
maincol.append(avgSlider) |
|
template.main.append(maincol) |
|
|
|
|
|
template.servable(title="Nan-Hsin Lin") |