Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting pie charts into bar charts in visualization #82

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions viz_scripts/generic_metrics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "narrative-hunter",
"id": "9c7ca9a4",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -36,7 +36,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "activated-portugal",
"id": "narrative-hunter",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -56,7 +56,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "alternative-voltage",
"id": "activated-portugal",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -121,11 +121,16 @@
"file_name='ntrips_mode_confirm%s' % file_suffix\n",
"plot_title_no_quality= \"Number of trips for each mode (selected by users)\"\n",
"try:\n",
" \n",
" labels_mc = expanded_ct['Mode_confirm'].value_counts(dropna=True).keys().tolist()\n",
" values_mc = expanded_ct['Mode_confirm'].value_counts(dropna=True).tolist() \n",
" values_mc = expanded_ct['Mode_confirm'].value_counts(dropna=True).tolist()\n",
" \n",
" data = pd.DataFrame({\"Mode_confirm\": labels_mc,\n",
" \"Number of Trips\": values_mc})\n",
Comment on lines +128 to +129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why you have this copy-pasted before every line.
Can you please explain:

  • why we need to create a separate data frame for the visualization of bar charts when we did not for pie charts?
  • why this needs to be copy-pasted instead being refactored into common code with the same interface?

" plot_title = plot_title_no_quality+\"\\n\"+quality_text\n",
" pie_chart_mode(plot_title,labels_mc,values_mc,file_name)\n",
" alt_text = store_alt_text_pie(pd.DataFrame(values_mc, labels_mc), file_name, plot_title)\n",
" \n",
" barplot_mode(data,\"Mode_confirm\",\"Number of Trips\", plot_title,file_name)\n",
" alt_text = store_alt_text_bar(pd.DataFrame(data['Number of Trips'].values, data['Mode_confirm']), file_name, plot_title)\n",
" print(expanded_ct['Mode_confirm'].value_counts(dropna=True))\n",
"except:\n",
" generate_missing_plot(plot_title_no_quality,debug_df,file_name)\n",
Expand All @@ -149,8 +154,10 @@
" values_mc = expanded_ct.query(\"Trip_purpose == 'Work'\").Mode_confirm.value_counts(dropna=True).tolist()\n",
" commute_quality_text = scaffolding.get_quality_text(expanded_ct, expanded_ct.query(\"Trip_purpose == 'Work'\"), \"commute\", include_test_users)\n",
" plot_title= plot_title_no_quality+\"\\n\"+commute_quality_text\n",
" pie_chart_mode(plot_title,labels_mc,values_mc,file_name)\n",
" alt_text = store_alt_text_pie(pd.DataFrame(values_mc, labels_mc), file_name, plot_title)\n",
" data = pd.DataFrame({\"Mode_confirm\": labels_mc,\n",
" \"Number of Trips\": values_mc})\n",
" barplot_mode(data,\"Mode_confirm\",\"Number of Trips\", plot_title,file_name)\n",
" alt_text = store_alt_text_bar(pd.DataFrame(values_mc, labels_mc), file_name, plot_title)\n",
" print(expanded_ct.query(\"Trip_purpose == 'Work'\").Mode_confirm.value_counts(dropna=True))\n",
"except:\n",
" debug_df.loc[\"Commute_trips\"] = len(expanded_ct.query(\"Trip_purpose == 'Work'\")) if \"Trip_purpose\" in expanded_ct.columns else 0\n",
Expand Down Expand Up @@ -182,8 +189,11 @@
" labels_tp = expanded_ct['Trip_purpose'].value_counts(dropna=True).keys().tolist()\n",
" values_tp = expanded_ct['Trip_purpose'].value_counts(dropna=True).tolist()\n",
" plot_title= plot_title_no_quality+\"\\n\"+quality_text\n",
" pie_chart_purpose(plot_title,labels_tp,values_tp,file_name)\n",
" alt_text = store_alt_text_pie(pd.DataFrame(values_tp, labels_tp), file_name, plot_title)\n",
" \n",
" data = pd.DataFrame({\"Mode_confirm\": labels_tp,\n",
" \"Number of Trips\": values_tp})\n",
" barplot_purpose(data,\"Mode_confirm\",\"Number of Trips\", plot_title,file_name)\n",
" alt_text = store_alt_text_bar(pd.DataFrame(data['Number of Trips'].values, data['Mode_confirm']), file_name, plot_title)\n",
" print(expanded_ct['Trip_purpose'].value_counts(dropna=True))\n",
"except:\n",
" generate_missing_plot(plot_title_no_quality,debug_df,file_name)\n",
Expand Down Expand Up @@ -214,8 +224,11 @@
" values_d10 = expanded_ct.loc[(expanded_ct['distance_miles'] <= 10)].Mode_confirm.value_counts(dropna=True).tolist()\n",
" d10_quality_text = scaffolding.get_quality_text(expanded_ct, expanded_ct[expanded_ct['distance_miles'] <= 10], \"< 10 mile\", include_test_users)\n",
" plot_title= plot_title_no_quality+\"\\n\"+d10_quality_text\n",
" pie_chart_mode(plot_title,labels_d10,values_d10,file_name)\n",
" alt_text = store_alt_text_pie(pd.DataFrame(values_d10, labels_d10), file_name, plot_title)\n",
" \n",
" data = pd.DataFrame({\"Mode_confirm\": labels_d10,\n",
" \"Number of Trips\": values_d10})\n",
" barplot_mode(data,\"Mode_confirm\",\"Number of Trips\", plot_title,file_name)\n",
" alt_text = store_alt_text_bar(pd.DataFrame(data['Number of Trips'].values, data['Mode_confirm']), file_name, plot_title)\n",
" print(expanded_ct.loc[(expanded_ct['distance_miles'] <= 10)].Mode_confirm.value_counts(dropna=True))\n",
"except:\n",
" d10_df = expanded_ct.query(\"distance_miles <= 10\") if \"distance_miles\" in expanded_ct.columns else expanded_ct\n",
Expand Down Expand Up @@ -249,21 +262,10 @@
" miles.columns = ['Total (miles)', 'Count', 'Average (miles)']\n",
" miles = miles.reset_index()\n",
" miles =miles.sort_values(by=['Total (miles)'], ascending=False)\n",
"\n",
" #data\n",
" miles_dict = dict(zip(miles['Mode_confirm'], miles['Total (miles)']))\n",
"\n",
" labels_m = []\n",
" values_m = []\n",
"\n",
" for x, y in miles_dict.items():\n",
" labels_m.append(x)\n",
" values_m.append(y)\n",
Comment on lines -252 to -261
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this no longer required?

" \n",
" plot_title=\"Miles for each mode (selected by users)\\n%s\" % quality_text\n",
"\n",
" pie_chart_mode(plot_title,labels_m,values_m,file_name)\n",
" alt_text = store_alt_text_pie(pd.DataFrame(values_m, labels_m), file_name, plot_title)\n",
" barplot_mode(miles,\"Mode_confirm\",\"Total (miles)\", plot_title,file_name)\n",
" alt_text = store_alt_text_bar(pd.DataFrame(miles['Total (miles)'].values, miles['Mode_confirm']), file_name, plot_title)\n",
" print(miles)\n",
"except:\n",
" generate_missing_plot(plot_title_no_quality,debug_df,file_name)\n",
Expand Down
30 changes: 16 additions & 14 deletions viz_scripts/mode_specific_metrics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,11 @@
" labels_tp = data_eb['Trip_purpose'].value_counts(dropna=True).keys().tolist()\n",
" values_tp = data_eb['Trip_purpose'].value_counts(dropna=True).tolist()\n",
" plot_title= plot_title_no_quality+\"\\n\"+quality_text\n",
" pie_chart_purpose(plot_title,labels_tp,values_tp,file_name)\n",
" alt_text = store_alt_text_pie(pd.DataFrame(values_tp, labels_tp), file_name, plot_title)\n",
" data = pd.DataFrame({\"Trip_purpose\": labels_tp,\n",
" \"Number of Trips\": values_tp})\n",
" \n",
" barplot_purpose(data,\"Trip_purpose\",\"Number of Trips\", plot_title,file_name)\n",
" alt_text = store_alt_text_bar(pd.DataFrame(data['Number of Trips'].values, data['Trip_purpose']), file_name, plot_title)\n",
"except:\n",
" generate_missing_plot(plot_title_no_quality,debug_df,file_name)\n",
" alt_text = store_alt_text_missing(debug_df, file_name, plot_title_no_quality)"
Expand All @@ -193,8 +196,11 @@
" labels_eb = data_eb.Replaced_mode.value_counts(dropna=True).keys().tolist()\n",
" values_eb = data_eb.Replaced_mode.value_counts(dropna=True).tolist()\n",
" plot_title= plot_title_no_quality+\"\\n\"+quality_text\n",
" pie_chart_mode(plot_title,labels_eb,values_eb,file_name)\n",
" alt_text = store_alt_text_pie(pd.DataFrame(values_eb, labels_eb), file_name, plot_title)\n",
" \n",
" data = pd.DataFrame({\"Replaced_mode\": labels_eb,\n",
" \"Number of Trips\": values_eb})\n",
" barplot_mode(data,\"Replaced_mode\",\"Number of Trips\", plot_title,file_name)\n",
" alt_text = store_alt_text_bar(pd.DataFrame(data['Number of Trips'].values, data['Replaced_mode']), file_name, plot_title)\n",
"except:\n",
" generate_missing_plot(plot_title_no_quality,debug_df,file_name)\n",
" alt_text = store_alt_text_missing(debug_df, file_name, plot_title_no_quality)"
Expand Down Expand Up @@ -224,17 +230,13 @@
" dg = dg.reset_index()\n",
" dg = dg.sort_values(by=['Total (miles)'], ascending=False)\n",
"\n",
" dg_dict = dict(zip(dg['Replaced_mode'], dg['Total (miles)']))\n",
" labels_m = []\n",
" values_m = []\n",
"\n",
" for x, y in dg_dict.items():\n",
" labels_m.append(x)\n",
" values_m.append(y)\n",
"\n",
" plot_title= plot_title_no_quality+\"\\n\"+quality_text\n",
" pie_chart_mode(plot_title,labels_m,values_m,file_name)\n",
" alt_text = store_alt_text_pie(pd.DataFrame(values_m, labels_m), file_name, plot_title)\n",
" \n",
" data = pd.DataFrame({\"Replaced_mode\": labels_eb,\n",
" \"Total (miles)\": values_eb})\n",
" \n",
" barplot_mode(dg,\"Replaced_mode\",\"Total (miles)\", plot_title,file_name)\n",
" alt_text = store_alt_text_bar(pd.DataFrame(dg['Total (miles)'].values, dg['Replaced_mode']), file_name, plot_title)\n",
" print(dg)\n",
"except:\n",
" generate_missing_plot(plot_title_no_quality,debug_df,file_name)\n",
Expand Down
29 changes: 29 additions & 0 deletions viz_scripts/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,35 @@ def barplot_mode(data,x,y,plot_title,file_name):
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.savefig(SAVE_DIR+file_name+".png", bbox_inches='tight')

def barplot_purpose(data,x,y,plot_title,file_name):
all_labels= ['Work',
'Home',
'Meal',
'Shopping',
'Personal/Medical',
'Recreation/Exercise',
'Transit transfer',
'Pick-up/Drop off',
'Entertainment/Social',
'Other',
'School',
'Religious',
'No travel',
'not_a_trip']

colours = dict(zip(all_labels, plt.cm.tab20.colors[:len(all_labels)]))
sns.set(font_scale=1.5)
f = plt.subplots(figsize=(15, 6))
sns.set(style='whitegrid')
ax = sns.barplot(x=x, y=y, palette=colours,data=data, ci=None)
plt.xlabel(x, fontsize=23)
plt.ylabel(y, fontsize=23)
plt.title(plot_title, fontsize=25)
# y should be based on the max range + the biggest label ("Gas Car, with others")
plt.text(0,-(data[y].max()/8 + 3.3),f"Last updated {arrow.get()}", fontsize=10)
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.savefig(SAVE_DIR+file_name+".png", bbox_inches='tight')

def barplot_mode2(data,x,y,y2,plot_title,file_name):
all_labels= ['Gas Car, drove alone',
'Bus',
Expand Down