|
1620 | 1620 | ], |
1621 | 1621 | "source": [ |
1622 | 1622 | "stackoverflow_monthly.matplotlib.plot(\n", |
1623 | | - " figsize=(8, 2), xlabel='creation date', ylabel='total questions', \n", |
| 1623 | + " figsize=(8, 2), xlabel='creation date', ylabel='total questions',\n", |
1624 | 1624 | " title='Matplotlib Questions per Month\\n(since the creation of Stack Overflow)'\n", |
1625 | 1625 | ")" |
1626 | 1626 | ] |
|
1774 | 1774 | "The `Figure` object is the container for all components of our visualization. It contains one or more `Axes` objects, which can be thought of as the (sub)plots, as well as other [*Artists*](https://matplotlib.org/stable/tutorials/intermediate/artists.html), which draw on the plot canvas (x-axis, y-axis, legend, lines, etc.). The following image from the Matplotlib documentation illustrates the different components of a figure:\n", |
1775 | 1775 | "\n", |
1776 | 1776 | "<div style=\"text-align: center;\">\n", |
1777 | | - " <img width=\"40%\" src=\"https://raw.githubusercontent.com/stefmolin/python-data-viz-workshop/main/media/figure_anatomy.svg\" alt=\"Matplotlib figure anatomy\" style=\"min-width: 400px\">\n", |
| 1777 | + " <img width=\"40%\" src=\"media/figure_anatomy.svg\" alt=\"Matplotlib figure anatomy\" style=\"min-width: 400px\">\n", |
1778 | 1778 | " <div><small><em><a href=\"https://matplotlib.org/stable/tutorials/introductory/quick_start.html#parts-of-a-figure\">Source</a></em></small></div>\n", |
1779 | 1779 | "</div>" |
1780 | 1780 | ] |
|
5750 | 5750 | ], |
5751 | 5751 | "source": [ |
5752 | 5752 | "ax = stackoverflow_monthly.matplotlib.plot(\n", |
5753 | | - " figsize=(8, 2), xlabel='creation date', ylabel='total questions', \n", |
| 5753 | + " figsize=(8, 2), xlabel='creation date', ylabel='total questions',\n", |
5754 | 5754 | " title='Matplotlib Questions per Month\\n(since the creation of Stack Overflow)'\n", |
5755 | 5755 | ")\n", |
5756 | 5756 | "ax.set_ylim(0, None) # this can also be done with pandas\n", |
|
21101 | 21101 | "fig, ax = plt.subplots(figsize=(8, 2))\n", |
21102 | 21102 | "ax.plot(avgs.index, avgs.matplotlib)\n", |
21103 | 21103 | "ax.fill_between(\n", |
21104 | | - " avgs.index, avgs.matplotlib - 2 * stds.matplotlib, \n", |
| 21104 | + " avgs.index, avgs.matplotlib - 2 * stds.matplotlib,\n", |
21105 | 21105 | " avgs.matplotlib + 2 * stds.matplotlib, alpha=0.25\n", |
21106 | 21106 | ")" |
21107 | 21107 | ] |
|
33882 | 33882 | "fig, ax = plt.subplots(figsize=(8, 2))\n", |
33883 | 33883 | "ax.plot(avgs.index, avgs.matplotlib)\n", |
33884 | 33884 | "ax.fill_between(\n", |
33885 | | - " avgs.index, avgs.matplotlib - 2 * stds.matplotlib, \n", |
| 33885 | + " avgs.index, avgs.matplotlib - 2 * stds.matplotlib,\n", |
33886 | 33886 | " avgs.matplotlib + 2 * stds.matplotlib, alpha=0.25\n", |
33887 | 33887 | ")\n", |
33888 | 33888 | "\n", |
|
34658 | 34658 | "fig, ax = plt.subplots(figsize=(9, 3))\n", |
34659 | 34659 | "ax.plot(\n", |
34660 | 34660 | " stackoverflow_monthly.index,\n", |
34661 | | - " stackoverflow_monthly.matplotlib, \n", |
| 34661 | + " stackoverflow_monthly.matplotlib,\n", |
34662 | 34662 | " 'ok', label=None, alpha=0.5\n", |
34663 | 34663 | ")" |
34664 | 34664 | ] |
|
35463 | 35463 | "source": [ |
35464 | 35464 | "fig, ax = plt.subplots(figsize=(9, 3))\n", |
35465 | 35465 | "ax.plot(\n", |
35466 | | - " x_axis_dates, stackoverflow_monthly.matplotlib, \n", |
| 35466 | + " x_axis_dates, stackoverflow_monthly.matplotlib,\n", |
35467 | 35467 | " 'ok', label=None, alpha=0.5\n", |
35468 | 35468 | ")" |
35469 | 35469 | ] |
|
40384 | 40384 | " ax.xaxis.set_major_formatter(mdates.DateFormatter('%b\\n%Y'))\n", |
40385 | 40385 | "\n", |
40386 | 40386 | " ax.fill_between(\n", |
40387 | | - " la_tavg.index, la_tavg, nyc_tavg, where=nyc_tavg > la_tavg, \n", |
| 40387 | + " la_tavg.index, la_tavg, nyc_tavg, where=nyc_tavg > la_tavg,\n", |
40388 | 40388 | " hatch='///', facecolor='gray', alpha=0.5, label='NYC hotter than LA'\n", |
40389 | 40389 | " )\n", |
40390 | 40390 | " ax.legend(ncols=3, loc='lower center')\n", |
|
47037 | 47037 | "source": [ |
47038 | 47038 | "fig, ax = plt.subplots(figsize=(12, 3))\n", |
47039 | 47039 | "ax.stackplot(\n", |
47040 | | - " mdates.date2num(top_libraries_monthly.index), top_libraries_monthly.to_numpy().T, \n", |
| 47040 | + " mdates.date2num(top_libraries_monthly.index), top_libraries_monthly.to_numpy().T,\n", |
47041 | 47041 | " labels=top_libraries_monthly.columns\n", |
47042 | 47042 | ")\n", |
47043 | 47043 | "ax.set(xlabel='', ylabel='tagged questions', title='Stack Overflow Questions per Month')\n", |
|
47070 | 47070 | " fig, ax = plt.subplots(figsize=(12, 3))\n", |
47071 | 47071 | " ax.stackplot(\n", |
47072 | 47072 | " mdates.date2num(data.index),\n", |
47073 | | - " data.to_numpy().T, \n", |
| 47073 | + " data.to_numpy().T,\n", |
47074 | 47074 | " labels=data.columns\n", |
47075 | 47075 | " )\n", |
47076 | 47076 | " ax.set(\n", |
|
49756 | 49756 | " fig, ax = plt.subplots(figsize=(12, 3))\n", |
49757 | 49757 | " ax.stackplot(\n", |
49758 | 49758 | " mdates.date2num(data.index),\n", |
49759 | | - " data.to_numpy().T, \n", |
| 49759 | + " data.to_numpy().T,\n", |
49760 | 49760 | " labels=data.columns\n", |
49761 | 49761 | " )\n", |
49762 | 49762 | " ax.set(\n", |
|
52394 | 52394 | "import datetime as dt\n", |
52395 | 52395 | "\n", |
52396 | 52396 | "ax = area_plot(top_libraries_monthly)\n", |
52397 | | - " \n", |
| 52397 | + "\n", |
52398 | 52398 | "# mark when seaborn was created\n", |
52399 | 52399 | "seaborn_released = dt.date(2013, 10, 28)\n", |
52400 | 52400 | "ax.axvline(seaborn_released, ymax=0.6, color='gray', linestyle='dashed')\n", |
|
57720 | 57720 | ")\n", |
57721 | 57721 | "middle = (seaborn_released - first_seaborn_qs) / 2 + first_seaborn_qs\n", |
57722 | 57722 | "ax.annotate(\n", |
57723 | | - " 'posts retroactively\\ntagged \"seaborn\"', \n", |
57724 | | - " xy=(mdates.date2num(middle), 3500), \n", |
| 57723 | + " 'posts retroactively\\ntagged \"seaborn\"',\n", |
| 57724 | + " xy=(mdates.date2num(middle), 3500),\n", |
57725 | 57725 | " va='top', ha='center'\n", |
57726 | 57726 | ")" |
57727 | 57727 | ] |
|
57773 | 57773 | " fig, ax = plt.subplots(figsize=(12, 3))\n", |
57774 | 57774 | " ax.stackplot(\n", |
57775 | 57775 | " mdates.date2num(data.index),\n", |
57776 | | - " data.to_numpy().T, \n", |
| 57776 | + " data.to_numpy().T,\n", |
57777 | 57777 | " labels=data.columns\n", |
57778 | 57778 | " )\n", |
57779 | 57779 | " ax.set(\n", |
|
66200 | 66200 | "\n", |
66201 | 66201 | "total_qs = top_libraries_monthly.sum()\n", |
66202 | 66202 | "inset_ax.barh(\n", |
66203 | | - " total_qs.index, total_qs.to_numpy(), \n", |
| 66203 | + " total_qs.index, total_qs.to_numpy(),\n", |
66204 | 66204 | " color=[colors[label] for label in total_qs.index]\n", |
66205 | 66205 | ")\n", |
66206 | 66206 | "inset_ax.yaxis.set_inverted(True)\n", |
|
72555 | 72555 | " co_occurring_library = data[library]\n", |
72556 | 72556 | " ax.barh(libraries, co_occurring_library, label=library, left=last)\n", |
72557 | 72557 | " last += co_occurring_library\n", |
72558 | | - " \n", |
| 72558 | + "\n", |
72559 | 72559 | " ax.yaxis.set_inverted(True)\n", |
72560 | 72560 | " return despine(ax)" |
72561 | 72561 | ] |
|
77939 | 77939 | " co_occurring_library = data[library]\n", |
77940 | 77940 | " ax.barh(libraries, co_occurring_library, label=library, left=last)\n", |
77941 | 77941 | " last += co_occurring_library\n", |
77942 | | - " \n", |
| 77942 | + "\n", |
77943 | 77943 | " ax.yaxis.set_inverted(True)\n", |
77944 | 77944 | " ax.legend(bbox_to_anchor=(1.35, 0.5), loc='center right', framealpha=0.5)\n", |
77945 | 77945 | " ax.set(xlabel='percentage of questions with co-occurrences', xlim=(0, 1))\n", |
@@ -80347,11 +80347,11 @@ |
80347 | 80347 | " for i, library in enumerate(libraries):\n", |
80348 | 80348 | " co_occurring_library = data[library]\n", |
80349 | 80349 | " ax.barh(\n", |
80350 | | - " libraries, co_occurring_library, \n", |
| 80350 | + " libraries, co_occurring_library,\n", |
80351 | 80351 | " label=library, left=last, color=cmap(i)\n", |
80352 | 80352 | " )\n", |
80353 | 80353 | " last += co_occurring_library\n", |
80354 | | - " \n", |
| 80354 | + "\n", |
80355 | 80355 | " ax.yaxis.set_inverted(True)\n", |
80356 | 80356 | " ax.legend(bbox_to_anchor=(1.35, 0.5), loc='center right', framealpha=0.5)\n", |
80357 | 80357 | " ax.set(xlabel='percentage of questions with co-occurrences', xlim=(0, 1))\n", |
|
82836 | 82836 | " )\n", |
82837 | 82837 | " last += prcp\n", |
82838 | 82838 | "\n", |
82839 | | - " ax.set_xlabel('2020 total precipitation (inches)') \n", |
| 82839 | + " ax.set_xlabel('2020 total precipitation (inches)')\n", |
82840 | 82840 | " ax.set_title('Total Precipitation per City in 2020', y=1.1)\n", |
82841 | 82841 | " ax.axvline(total_prcp['Seattle'], linestyle='--', color='gray')\n", |
82842 | 82842 | " ax.legend(bbox_to_anchor=(0.5, 1.15), loc='upper center', ncols=4, frameon=False)\n", |
|
84824 | 84824 | ], |
84825 | 84825 | "source": [ |
84826 | 84826 | "subway = pd.read_csv(\n", |
84827 | | - " '../data/NYC_subway_daily.csv', parse_dates=['Datetime'], \n", |
| 84827 | + " '../data/NYC_subway_daily.csv', parse_dates=['Datetime'],\n", |
84828 | 84828 | " index_col=['Borough', 'Datetime']\n", |
84829 | 84829 | ")\n", |
84830 | 84830 | "subway_daily = subway.unstack(0)\n", |
|
90733 | 90733 | "\n", |
90734 | 90734 | " axes[0].set_ylabel('Frequency')\n", |
90735 | 90735 | " fig.suptitle('Histogram of Daily Subway Entries in Manhattan')\n", |
90736 | | - " \n", |
| 90736 | + "\n", |
90737 | 90737 | " return fig, axes" |
90738 | 90738 | ] |
90739 | 90739 | }, |
|
0 commit comments