From e4ba004fec5bf2ae480d51e16a693bc402d3c2f3 Mon Sep 17 00:00:00 2001 From: scuti Date: Sat, 19 Apr 2025 10:23:42 -0700 Subject: [PATCH] Added param. to save graphs to file. --- stackoverflow-survey.ipynb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/stackoverflow-survey.ipynb b/stackoverflow-survey.ipynb index d2ce02c..d94cb92 100644 --- a/stackoverflow-survey.ipynb +++ b/stackoverflow-survey.ipynb @@ -59,7 +59,7 @@ " )\n", " return langs_by_popularity\n", "\n", - "def visualize_langs(langs, langs2, label1 = \"condition1\", label2 = \"condition2\"):\n", + "def visualize_langs(langs, langs2, label1 = \"condition1\", label2 = \"condition2\", saveto=None):\n", " DOT_COLOR1 = \"lightblue\"\n", " DOT_COLOR2 = \"red\"\n", " BG_COLOR = \"black\" \n", @@ -88,15 +88,21 @@ " \n", " plt.grid(axis='x', linestyle='--', alpha=0.75) \n", " plt.title(\"%s vs %s\" % (label1, label2))\n", + " if saveto is not None:\n", + " plt.savefig(saveto)\n", " del df, df2\n", "\n", "l1 = get_langs( so_df )\n", "l2 = get_langs( so_df, \"LanguageWantToWorkWith\" )\n", - "visualize_langs(l1,l2, label1=\"have worked with\", label2=\"want to work with\")\n", + "visualize_langs(l1,l2, \n", + " label1=\"have worked with\", label2=\"want to work with\",\n", + " saveto=\"images/used-vs-want2use.png\")\n", "\n", "l3 = get_langs( so_df, \"LanguageAdmired\")\n", "l4 = get_langs( so_df, \"LanguageWantToWorkWith\")\n", - "visualize_langs(l3, l4, label1=\"admired\", label2=\"want to work with\")\n", + "visualize_langs(l3, l4, \n", + " label1=\"admired\", label2=\"want to work with\",\n", + " saveto=\"images/admired-vs-want2use.png\")\n", " " ] }, @@ -118,7 +124,7 @@ " result[key] = dict1[key] - dict2[key]\n", " return result\n", "\n", - "def visualize_diff(diff_dict, color=\"lightblue\"):\n", + "def visualize_diff(diff_dict, color=\"lightblue\", saveto=None):\n", " diff_sorted = dict(\n", " sorted(diff_dict.items(), key=lambda item: item[1], reverse=True)\n", " )\n", @@ -145,11 +151,12 @@ " if len(positive_values) < len(df):\n", " # don't draw the line if every value is greater than 0\n", " plt.axhline(y=df[KEY].tolist().index(lowest) + offset, color='red', linestyle='--')\n", - " pass\n", + " if saveto is not None:\n", + " plt.savefig(saveto)\n", " \n", "motiv_diff = get_difference(l2, l1)\n", "# print(motiv_diff)\n", - "visualize_diff(motiv_diff)\n", + "visualize_diff(motiv_diff, saveto=\"images/delta.png\")\n", "\n", "# determine level of hype\n", "hype = get_difference(l4, l3)\n",