Skip to content

Commit

Permalink
fix the docs build and add an easy example for subplots using seaborn
Browse files Browse the repository at this point in the history
- also add a copy button to all code block for easier usage of the docs
  • Loading branch information
jkittner authored and ocefpaf committed Jun 12, 2023
1 parent 93c8c69 commit 4cd2b3c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Option libraries:
- Scipy http://www.scipy.org/ (to fit data with Weibull distribution)
- ffmpeg https://www.ffmpeg.org/ (to output video)
- click http://click.pocoo.org/ (for command line interface tools)
- seaborn https://seaborn.pydata.org/ (for easy subplots)

### Install latest release version via pip

Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"sphinx.ext.intersphinx",
"sphinx.ext.napoleon",
"nbsphinx",
"sphinx_copybutton",
]

# Add any paths that contain templates here, relative to this directory.
Expand Down
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ https://docs.github.com/en/pull-requests/collaborating-with-pull-requests
:target: https://github.com/python-windrose/windrose/actions/workflows/tests.yml
.. |DOI| image:: https://zenodo.org/badge/37549137.svg
:target: https://zenodo.org/badge/latestdoi/37549137
.. |Research software impact| image:: http://depsy.org/api/package/pypi/windrose/badge.svg
:target: http://depsy.org/package/python/windrose

Indices and tables
==================
Expand Down
1 change: 1 addition & 0 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Option libraries:
- SciPy https://scipy.org/ (to fit data with Weibull distribution)
- ffmpeg https://www.ffmpeg.org/ (to output video)
- click https://click.palletsprojects.com/ (for command line interface tools)
- seaborn https://seaborn.pydata.org/ (for easy subplots)

Install latest release version via pip
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
83 changes: 76 additions & 7 deletions notebooks/usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"\n",
"ax = WindroseAxes.from_ax()\n",
"ax.bar(wd, ws, normed=True, opening=0.8, edgecolor=\"white\")\n",
"ax.set_legend();"
"ax.set_legend()"
]
},
{
Expand All @@ -60,7 +60,7 @@
"source": [
"ax = WindroseAxes.from_ax()\n",
"ax.box(wd, ws, bins=np.arange(0, 8, 1))\n",
"ax.set_legend();"
"ax.set_legend()"
]
},
{
Expand All @@ -80,7 +80,7 @@
"\n",
"ax = WindroseAxes.from_ax()\n",
"ax.contourf(wd, ws, bins=np.arange(0, 8, 1), cmap=cm.hot)\n",
"ax.set_legend();"
"ax.set_legend()"
]
},
{
Expand All @@ -99,7 +99,7 @@
"ax = WindroseAxes.from_ax()\n",
"ax.contourf(wd, ws, bins=np.arange(0, 8, 1), cmap=cm.hot)\n",
"ax.contour(wd, ws, bins=np.arange(0, 8, 1), colors=\"black\")\n",
"ax.set_legend();"
"ax.set_legend()"
]
},
{
Expand All @@ -117,7 +117,7 @@
"source": [
"ax = WindroseAxes.from_ax()\n",
"ax.contour(wd, ws, bins=np.arange(0, 8, 1), cmap=cm.hot, lw=3)\n",
"ax.set_legend();"
"ax.set_legend()"
]
},
{
Expand Down Expand Up @@ -356,6 +356,75 @@
" ax.tick_params(labelleft=False, labelbottom=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Subplots\n",
"\n",
"[seaborn](https://seaborn.pydata.org/index.html) offers an easy way to create subplots per parameter. For example per month or day. You can adapt this to have years as columns and rows as months or vice versa."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from windrose import WindroseAxes, plot_windrose\n",
"import seaborn as sns\n",
"from matplotlib import pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"wind_data = pd.DataFrame({\n",
" 'ws': np.random.random(1200) * 6,\n",
" 'wd': np.random.random(1200) * 360,\n",
" 'month': np.repeat(range(1, 13), 100),\n",
"})\n",
"\n",
"\n",
"def plot_windrose_subplots(data, *, direction, var, color=None, **kwargs):\n",
" \"\"\"wrapper function to create subplots per axis\"\"\"\n",
" ax = plt.gca()\n",
" ax = WindroseAxes.from_ax(ax=ax)\n",
" plot_windrose(direction_or_df=data[direction], var=data[var], ax=ax, **kwargs)\n",
"\n",
"# this creates the raw subplot structure with a subplot per value in month.\n",
"g = sns.FacetGrid(\n",
" data=wind_data,\n",
" # the column name for each level a subplot should be created\n",
" col='month',\n",
" # place a maximum of 3 plots per row\n",
" col_wrap=3,\n",
" subplot_kws={'projection': 'windrose'},\n",
" sharex=False,\n",
" sharey=False,\n",
" despine=False,\n",
" height=3.5\n",
")\n",
"\n",
"g.map_dataframe(\n",
" plot_windrose_subplots,\n",
" direction='wd',\n",
" var='ws',\n",
" normed=True,\n",
" # manually set bins, so they match for each subplot\n",
" bins=(0.1, 1, 2, 3, 4, 5),\n",
" calm_limit=0.1,\n",
" kind='bar'\n",
")\n",
"\n",
"# make the subplots easier to compare, by having the same y-axis range\n",
"y_ticks = range(0, 17, 4)\n",
"for ax in g.axes:\n",
" ax.set_legend(title='$m \\cdot s^{-1}$', bbox_to_anchor=(1.15, -.1), loc='lower right')\n",
" ax.set_rgrids(y_ticks, y_ticks)\n",
"\n",
"# adjust the spacing between the subplots to have sufficient space between plots\n",
"plt.subplots_adjust(wspace=-.2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -402,7 +471,7 @@
"ws = np.random.random(N) * 6\n",
"wd = np.random.random(N) * 360\n",
"df = pd.DataFrame({\"speed\": ws, \"direction\": wd})\n",
"plot_windrose(df, kind=\"contour\", bins=np.arange(0.01, 8, 1), cmap=cm.hot, lw=3);"
"plot_windrose(df, kind=\"contour\", bins=np.arange(0.01, 8, 1), cmap=cm.hot, lw=3)"
]
},
{
Expand Down Expand Up @@ -470,7 +539,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.11.3"
}
},
"nbformat": 4,
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ pytest-cov
pytest-flake8
pytest-mpl
pytest-sugar
seaborn
setuptools_scm
sphinx
sphinx-copybutton
sphinx_rtd_theme
twine
wheel

0 comments on commit 4cd2b3c

Please sign in to comment.