From 67c90c91d385e0461a89c6266133397ff1b1ecc6 Mon Sep 17 00:00:00 2001 From: Louis Oliphant <ltoliphant@wisc.edu> Date: Wed, 1 May 2024 00:54:42 -0500 Subject: [PATCH] Louis Lecture 38 Plotting 3 --- .../Lec38_Plotting3_Solution_Oliphant.ipynb | 238 +++++++++++++----- .../Lec38_Plotting3_Template_Oliphant.ipynb | 35 ++- 2 files changed, 209 insertions(+), 64 deletions(-) diff --git a/s24/Louis_Lecture_Notes/38_Plotting3/Lec38_Plotting3_Solution_Oliphant.ipynb b/s24/Louis_Lecture_Notes/38_Plotting3/Lec38_Plotting3_Solution_Oliphant.ipynb index 60acaee..f53ec88 100644 --- a/s24/Louis_Lecture_Notes/38_Plotting3/Lec38_Plotting3_Solution_Oliphant.ipynb +++ b/s24/Louis_Lecture_Notes/38_Plotting3/Lec38_Plotting3_Solution_Oliphant.ipynb @@ -1448,7 +1448,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -1576,7 +1576,7 @@ "total 0.967913 1.000000 " ] }, - "execution_count": 12, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -1595,7 +1595,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -1637,7 +1637,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -1657,7 +1657,7 @@ "dtype: int64" ] }, - "execution_count": 14, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" }, @@ -1687,7 +1687,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -1707,7 +1707,7 @@ "dtype: int64" ] }, - "execution_count": 15, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" }, @@ -1738,7 +1738,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -1747,7 +1747,7 @@ "Text(0, 0.5, '# Craft Breweries')" ] }, - "execution_count": 16, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" }, @@ -1780,7 +1780,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -1800,7 +1800,7 @@ "dtype: int64" ] }, - "execution_count": 17, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" }, @@ -1831,7 +1831,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -1851,7 +1851,7 @@ "dtype: int64" ] }, - "execution_count": 18, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" }, @@ -1887,7 +1887,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -1996,7 +1996,7 @@ "11 30 16" ] }, - "execution_count": 19, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -2021,7 +2021,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -2056,7 +2056,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -2065,7 +2065,7 @@ "Text(0, 0.5, 'Temp (Fahrenheit)')" ] }, - "execution_count": 21, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" }, @@ -2105,7 +2105,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -2214,7 +2214,7 @@ "11 -1.111111 -8.888889" ] }, - "execution_count": 22, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -2227,6 +2227,128 @@ "celcius_df" ] }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<div>\n", + "<style scoped>\n", + " .dataframe tbody tr th:only-of-type {\n", + " vertical-align: middle;\n", + " }\n", + "\n", + " .dataframe tbody tr th {\n", + " vertical-align: top;\n", + " }\n", + "\n", + " .dataframe thead th {\n", + " text-align: right;\n", + " }\n", + "</style>\n", + "<table border=\"1\" class=\"dataframe\">\n", + " <thead>\n", + " <tr style=\"text-align: right;\">\n", + " <th></th>\n", + " <th>high</th>\n", + " <th>low</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>0</th>\n", + " <td>-3.333333</td>\n", + " <td>-11.666667</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1</th>\n", + " <td>-0.555556</td>\n", + " <td>-9.444444</td>\n", + " </tr>\n", + " <tr>\n", + " <th>2</th>\n", + " <td>6.111111</td>\n", + " <td>-3.888889</td>\n", + " </tr>\n", + " <tr>\n", + " <th>3</th>\n", + " <td>13.888889</td>\n", + " <td>2.222222</td>\n", + " </tr>\n", + " <tr>\n", + " <th>4</th>\n", + " <td>20.000000</td>\n", + " <td>7.777778</td>\n", + " </tr>\n", + " <tr>\n", + " <th>5</th>\n", + " <td>25.555556</td>\n", + " <td>13.333333</td>\n", + " </tr>\n", + " <tr>\n", + " <th>6</th>\n", + " <td>27.777778</td>\n", + " <td>16.111111</td>\n", + " </tr>\n", + " <tr>\n", + " <th>7</th>\n", + " <td>26.111111</td>\n", + " <td>15.000000</td>\n", + " </tr>\n", + " <tr>\n", + " <th>8</th>\n", + " <td>22.222222</td>\n", + " <td>10.000000</td>\n", + " </tr>\n", + " <tr>\n", + " <th>9</th>\n", + " <td>15.000000</td>\n", + " <td>3.888889</td>\n", + " </tr>\n", + " <tr>\n", + " <th>10</th>\n", + " <td>6.666667</td>\n", + " <td>-2.222222</td>\n", + " </tr>\n", + " <tr>\n", + " <th>11</th>\n", + " <td>-1.111111</td>\n", + " <td>-8.888889</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " high low\n", + "0 -3.333333 -11.666667\n", + "1 -0.555556 -9.444444\n", + "2 6.111111 -3.888889\n", + "3 13.888889 2.222222\n", + "4 20.000000 7.777778\n", + "5 25.555556 13.333333\n", + "6 27.777778 16.111111\n", + "7 26.111111 15.000000\n", + "8 22.222222 10.000000\n", + "9 15.000000 3.888889\n", + "10 6.666667 -2.222222\n", + "11 -1.111111 -8.888889" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# or do it using the apply() function\n", + "celcius_df = temp_df.apply(f_to_c)\n", + "celcius_df" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -2236,7 +2358,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -2345,13 +2467,13 @@ "11 -1.111111 -8.888889" ] }, - "execution_count": 20, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "celcius_df = (lambda f: (f - 32)*5/9)(temp_df)\n", + "celcius_df = temp_df.apply(lambda f: (f - 32)*5/9)\n", "celcius_df" ] }, @@ -2364,7 +2486,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 22, "metadata": {}, "outputs": [ { @@ -2486,7 +2608,7 @@ "11 -1.111111 -8.888889 0" ] }, - "execution_count": 21, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -2505,7 +2627,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -2541,7 +2663,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -2753,13 +2875,13 @@ "[5 rows x 25 columns]" ] }, - "execution_count": 23, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "hdf = pd.read_csv(\"Fire_Hydrants.csv\")\n", + "hdf = pd.read_csv(\"fire_hydrants.csv\")\n", "hdf.tail()" ] }, @@ -2772,7 +2894,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 25, "metadata": {}, "outputs": [ { @@ -2787,7 +2909,7 @@ " dtype='object')" ] }, - "execution_count": 24, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } @@ -2813,7 +2935,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 26, "metadata": {}, "outputs": [ { @@ -2835,7 +2957,7 @@ "Name: count, dtype: int64" ] }, - "execution_count": 25, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -2862,7 +2984,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -2878,7 +3000,7 @@ "Name: count, dtype: int64" ] }, - "execution_count": 26, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -2898,7 +3020,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 28, "metadata": {}, "outputs": [ { @@ -2907,7 +3029,7 @@ "Text(0, 0.5, 'Fire hydrant count')" ] }, - "execution_count": 27, + "execution_count": 28, "metadata": {}, "output_type": "execute_result" }, @@ -2951,7 +3073,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 29, "metadata": {}, "outputs": [ { @@ -2960,7 +3082,7 @@ "Text(0.5, 0, 'Hydrant Type')" ] }, - "execution_count": 28, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" }, @@ -3005,7 +3127,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 30, "metadata": {}, "outputs": [ { @@ -3025,7 +3147,7 @@ "Name: year_manufactured, Length: 3458, dtype: float64" ] }, - "execution_count": 29, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -3040,7 +3162,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 31, "metadata": {}, "outputs": [ { @@ -3060,7 +3182,7 @@ "Name: year_manufactured, Length: 6651, dtype: float64" ] }, - "execution_count": 30, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } @@ -3073,7 +3195,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 32, "metadata": {}, "outputs": [ { @@ -3093,7 +3215,7 @@ "Name: year_manufactured, Length: 3458, dtype: float64" ] }, - "execution_count": 31, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -3107,7 +3229,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 33, "metadata": {}, "outputs": [ { @@ -3127,7 +3249,7 @@ "Name: year_manufactured, Length: 6651, dtype: float64" ] }, - "execution_count": 32, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -3140,7 +3262,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -3155,7 +3277,7 @@ "Name: count, dtype: int64" ] }, - "execution_count": 33, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } @@ -3169,7 +3291,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 35, "metadata": {}, "outputs": [ { @@ -3189,7 +3311,7 @@ "Name: count, dtype: int64" ] }, - "execution_count": 34, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } @@ -3203,7 +3325,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 36, "metadata": {}, "outputs": [ { @@ -3306,7 +3428,7 @@ "2010 503.0 1196" ] }, - "execution_count": 35, + "execution_count": 36, "metadata": {}, "output_type": "execute_result" } @@ -3322,7 +3444,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 37, "metadata": {}, "outputs": [ { @@ -3331,7 +3453,7 @@ "Text(0, 0.5, 'Hydrant Count')" ] }, - "execution_count": 36, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" }, @@ -3355,7 +3477,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 38, "metadata": {}, "outputs": [ { @@ -3364,7 +3486,7 @@ "Text(0, 0.5, 'Hydrant Count')" ] }, - "execution_count": 37, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" }, @@ -3388,7 +3510,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 39, "metadata": {}, "outputs": [ { diff --git a/s24/Louis_Lecture_Notes/38_Plotting3/Lec38_Plotting3_Template_Oliphant.ipynb b/s24/Louis_Lecture_Notes/38_Plotting3/Lec38_Plotting3_Template_Oliphant.ipynb index be4d1df..1240865 100644 --- a/s24/Louis_Lecture_Notes/38_Plotting3/Lec38_Plotting3_Template_Oliphant.ipynb +++ b/s24/Louis_Lecture_Notes/38_Plotting3/Lec38_Plotting3_Template_Oliphant.ipynb @@ -392,11 +392,22 @@ "celcius_df" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# or do it using the apply() function\n", + "celcius_df = temp_df.apply(f_to_c)\n", + "celcius_df" + ] + }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### We can also do that using a lambda function" + "#### Try writing a lambda function and using the apply() function to do the same thing" ] }, { @@ -405,8 +416,7 @@ "metadata": {}, "outputs": [], "source": [ - "celcius_df = (lambda f: (f - 32)*5/9)(temp_df)\n", - "celcius_df" + "#TODO -- write your solution here" ] }, { @@ -464,7 +474,7 @@ "metadata": {}, "outputs": [], "source": [ - "hdf = pd.read_csv(\"Fire_Hydrants.csv\")\n", + "hdf = pd.read_csv(\"fire_hydrants.csv\")\n", "hdf.tail()" ] }, @@ -481,7 +491,7 @@ "metadata": {}, "outputs": [], "source": [ - "# write your code here" + "# write your code here -- try typing hdf. (don't forget the dot) and pressing tab -- Anything pop up that looks like it might work?\n" ] }, { @@ -590,7 +600,20 @@ "metadata": {}, "outputs": [], "source": [ - "# write your code here" + "# write your code here\n", + "pacer_years=hdf[hdf['Style']=='Pacer']['year_manufactured']\n", + "other_years=hdf[hdf['Style']!='Pacer']['year_manufactured']\n", + "pacer_year=pacer_year.dropna()\n", + "other_year=other_year.dropna()\n", + "pacer_decades = pacer_year//10 * 10\n", + "pacer_decades = pacer_decades.value_counts()\n", + "other_decades = (other_year//10 * 10).value_counts()\n", + "plot_df = DataFrame({'Pacer':pacer_decades,'Other':other_decades})\n", + "plot_df.index=[int(x) for x in plot_df.index]\n", + "ax=plot_df.plot.bar()\n", + "ax.set_xlabel(\"Manufacture Year\")\n", + "ax=plot_df.plot.bar(stacked=True)\n", + "ax.set_xlabel(\"Manufacture Year\")" ] } ], -- GitLab