From 9d5629f80fc197f50cdd5ce1c905288a89a061d3 Mon Sep 17 00:00:00 2001 From: jnaiman Date: Wed, 9 Oct 2024 18:14:38 -0500 Subject: [PATCH] prep stuffs --- .../pages/2_Widget_Exploration.py | 86 +++++++++++++++++++ .../pages/2_Widget_Exploration.py | 86 +++++++++++++++++++ 2 files changed, 172 insertions(+) diff --git a/_site/week12/prepMaterials/pages/2_Widget_Exploration.py b/_site/week12/prepMaterials/pages/2_Widget_Exploration.py index f57d6f4..c0e3245 100644 --- a/_site/week12/prepMaterials/pages/2_Widget_Exploration.py +++ b/_site/week12/prepMaterials/pages/2_Widget_Exploration.py @@ -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 \ No newline at end of file diff --git a/week12/prepMaterials/pages/2_Widget_Exploration.py b/week12/prepMaterials/pages/2_Widget_Exploration.py index f57d6f4..c0e3245 100644 --- a/week12/prepMaterials/pages/2_Widget_Exploration.py +++ b/week12/prepMaterials/pages/2_Widget_Exploration.py @@ -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 \ No newline at end of file