Skip to content

Commit

Permalink
prep stuffs
Browse files Browse the repository at this point in the history
  • Loading branch information
jnaiman committed Oct 9, 2024
1 parent c54d50b commit 9d5629f
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
86 changes: 86 additions & 0 deletions _site/week12/prepMaterials/pages/2_Widget_Exploration.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,89 @@
st.write("You selected Streamlit!")
else:
st.write("You didn't select Streamlit but that's ok, Data Viz still likes you :grin:")

st.header('Connecting Plots and Widgets')

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from io import BytesIO


df = pd.read_csv("https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/mobility.csv")

# vertical alignment so they end up side by side
fig_col2, controls_col2 = st.columns([2,1], vertical_alignment='center')

bins = np.linspace(df['Student_teacher_ratio'].min(),df['Student_teacher_ratio'].max(), 10)
table = df.pivot_table(index='State', columns=pd.cut(df['Student_teacher_ratio'], bins), aggfunc='size')

# multi-select
states_selected2 = controls_col2.multiselect('Which states do you want to view?',
table.index.values)#, key='unik1155')
# had to pass unique key to have double widgets with same value

# range slider -- added
student_teacher_ratio_range = controls_col2.slider("Range of student teacher ratio:",
df['Student_teacher_ratio'].min(),
df['Student_teacher_ratio'].max(),
(0.25*df['Student_teacher_ratio'].mean(),
0.75*df['Student_teacher_ratio'].mean()))

# note all the "2's" here, probably will just update the original one
if len(states_selected2) > 0: # here we set a default value for the slider, so no need to have a tag
min_range = student_teacher_ratio_range[0] # added
max_range = student_teacher_ratio_range[1] # added

df_subset2 = df[(df['State'].isin(states_selected2)) & (df['Student_teacher_ratio'] >= min_range) & (df['Student_teacher_ratio']<=max_range)] # changed

# just 10 bins over the full range --> changed
bins2 = 10 #np.linspace(df['Student_teacher_ratio'].min(),df['Student_teacher_ratio'].max(), 10)

# make pivot table -- changed
table_sub2 = df_subset2.pivot_table(index='State',
columns=pd.cut(df_subset2['Student_teacher_ratio'], bins2),
aggfunc='size')

base_size = 4
fig2,ax2 = plt.subplots(figsize=(base_size,2*base_size)) # this changed too for different size
extent2 = [df_subset2['Student_teacher_ratio'].min(),
df_subset2['Student_teacher_ratio'].max(),
0, len(table_sub2.index)]
ax2.imshow(table_sub2.values, cmap='hot', interpolation='nearest', extent=extent2)
ax2.set_yticks(range(len(table_sub2.index)))
ax2.set_yticklabels(table_sub2.index)
#ax2.set_xticklabels()

buf2 = BytesIO()
fig2.tight_layout()
fig2.savefig(buf2, format="png")
fig_col2.image(buf2, width = 400) # changed here to fit better
else:
min_range = student_teacher_ratio_range[0] # added
max_range = student_teacher_ratio_range[1] # added

df_subset2 = df[(df['Student_teacher_ratio'] >= min_range) & (df['Student_teacher_ratio']<=max_range)] # changed

# just 10 bins over the full range --> changed
bins2 = 10 #np.linspace(df['Student_teacher_ratio'].min(),df['Student_teacher_ratio'].max(), 10)

# make pivot table -- changed
table_sub2 = df_subset2.pivot_table(index='State',
columns=pd.cut(df_subset2['Student_teacher_ratio'], bins2),
aggfunc='size')

base_size = 4
fig2,ax2 = plt.subplots(figsize=(base_size,2*base_size)) # this changed too for different size
extent2 = [df_subset2['Student_teacher_ratio'].min(),
df_subset2['Student_teacher_ratio'].max(),
0, len(table_sub2.index)]
ax2.imshow(table_sub2.values, cmap='hot', interpolation='nearest', extent=extent2)
ax2.set_yticks(range(len(table_sub2.index)))
ax2.set_yticklabels(table_sub2.index)
#ax2.set_xticklabels()

buf2 = BytesIO()
fig2.tight_layout()
fig2.savefig(buf2, format="png")
fig_col2.image(buf2, width = 400) # changed here to fit better
86 changes: 86 additions & 0 deletions week12/prepMaterials/pages/2_Widget_Exploration.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,89 @@
st.write("You selected Streamlit!")
else:
st.write("You didn't select Streamlit but that's ok, Data Viz still likes you :grin:")

st.header('Connecting Plots and Widgets')

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from io import BytesIO


df = pd.read_csv("https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/mobility.csv")

# vertical alignment so they end up side by side
fig_col2, controls_col2 = st.columns([2,1], vertical_alignment='center')

bins = np.linspace(df['Student_teacher_ratio'].min(),df['Student_teacher_ratio'].max(), 10)
table = df.pivot_table(index='State', columns=pd.cut(df['Student_teacher_ratio'], bins), aggfunc='size')

# multi-select
states_selected2 = controls_col2.multiselect('Which states do you want to view?',
table.index.values)#, key='unik1155')
# had to pass unique key to have double widgets with same value

# range slider -- added
student_teacher_ratio_range = controls_col2.slider("Range of student teacher ratio:",
df['Student_teacher_ratio'].min(),
df['Student_teacher_ratio'].max(),
(0.25*df['Student_teacher_ratio'].mean(),
0.75*df['Student_teacher_ratio'].mean()))

# note all the "2's" here, probably will just update the original one
if len(states_selected2) > 0: # here we set a default value for the slider, so no need to have a tag
min_range = student_teacher_ratio_range[0] # added
max_range = student_teacher_ratio_range[1] # added

df_subset2 = df[(df['State'].isin(states_selected2)) & (df['Student_teacher_ratio'] >= min_range) & (df['Student_teacher_ratio']<=max_range)] # changed

# just 10 bins over the full range --> changed
bins2 = 10 #np.linspace(df['Student_teacher_ratio'].min(),df['Student_teacher_ratio'].max(), 10)

# make pivot table -- changed
table_sub2 = df_subset2.pivot_table(index='State',
columns=pd.cut(df_subset2['Student_teacher_ratio'], bins2),
aggfunc='size')

base_size = 4
fig2,ax2 = plt.subplots(figsize=(base_size,2*base_size)) # this changed too for different size
extent2 = [df_subset2['Student_teacher_ratio'].min(),
df_subset2['Student_teacher_ratio'].max(),
0, len(table_sub2.index)]
ax2.imshow(table_sub2.values, cmap='hot', interpolation='nearest', extent=extent2)
ax2.set_yticks(range(len(table_sub2.index)))
ax2.set_yticklabels(table_sub2.index)
#ax2.set_xticklabels()

buf2 = BytesIO()
fig2.tight_layout()
fig2.savefig(buf2, format="png")
fig_col2.image(buf2, width = 400) # changed here to fit better
else:
min_range = student_teacher_ratio_range[0] # added
max_range = student_teacher_ratio_range[1] # added

df_subset2 = df[(df['Student_teacher_ratio'] >= min_range) & (df['Student_teacher_ratio']<=max_range)] # changed

# just 10 bins over the full range --> changed
bins2 = 10 #np.linspace(df['Student_teacher_ratio'].min(),df['Student_teacher_ratio'].max(), 10)

# make pivot table -- changed
table_sub2 = df_subset2.pivot_table(index='State',
columns=pd.cut(df_subset2['Student_teacher_ratio'], bins2),
aggfunc='size')

base_size = 4
fig2,ax2 = plt.subplots(figsize=(base_size,2*base_size)) # this changed too for different size
extent2 = [df_subset2['Student_teacher_ratio'].min(),
df_subset2['Student_teacher_ratio'].max(),
0, len(table_sub2.index)]
ax2.imshow(table_sub2.values, cmap='hot', interpolation='nearest', extent=extent2)
ax2.set_yticks(range(len(table_sub2.index)))
ax2.set_yticklabels(table_sub2.index)
#ax2.set_xticklabels()

buf2 = BytesIO()
fig2.tight_layout()
fig2.savefig(buf2, format="png")
fig_col2.image(buf2, width = 400) # changed here to fit better

0 comments on commit 9d5629f

Please sign in to comment.