Skip to content

Commit

Permalink
Merge pull request #185 from jukent/subtitle
Browse files Browse the repository at this point in the history
subtitle kwarg to set_titles_and_labels
  • Loading branch information
jukent authored Jan 17, 2024
2 parents b85ced6 + 84af690 commit e9b37d9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
31 changes: 30 additions & 1 deletion docs/examples/set_titles_and_labels.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Create plot\n",
"# Create plot with a subtitle\n",
"\n",
"# Create a figure\n",
"fig, ax = plt.subplots()\n",
Expand All @@ -55,6 +55,35 @@
"gv.set_titles_and_labels(ax,\n",
" maintitle=\"Title\",\n",
" maintitlefontsize=24,\n",
" subtitle=\"Subtitle\",\n",
" xlabel=\"x\",\n",
" ylabel=\"y\",\n",
" labelfontsize=16)\n",
"\n",
"# Show the plot\n",
"plt.show();"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create plot with left and right subtitles\n",
"\n",
"# Create a figure\n",
"fig, ax = plt.subplots()\n",
"\n",
"# Plot the example data\n",
"ax.plot(x, y)\n",
"\n",
"# Use geocat.viz.util convenience function to set titles and labels\n",
"gv.set_titles_and_labels(ax,\n",
" maintitle=\"Title\",\n",
" maintitlefontsize=24,\n",
" lefttitle=\"Left title\",\n",
" righttitle='Right title',\n",
" xlabel=\"x\",\n",
" ylabel=\"y\",\n",
" labelfontsize=16)\n",
Expand Down
5 changes: 4 additions & 1 deletion docs/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
Release Notes
=============

v2023.11.0 (Unreleased)
v2024.1.0 (Unreleased)
-----------------------

New Features
^^^^^^^^^^^^
* Added subtitle functionality to `set_titles_and_labels()` by `Julia Kent`_ in (:pr:`185`)

Documentation
^^^^^^^^^^^^^
Expand Down
16 changes: 15 additions & 1 deletion src/geocat/viz/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ def set_titles_and_labels(ax: typing.Union[matplotlib.axes.Axes,
cartopy.mpl.geoaxes.GeoAxesSubplot],
maintitle: str = None,
maintitlefontsize: int = 18,
subtitle: str = None,
subtitlefontsize: int = 18,
lefttitle: str = None,
lefttitlefontsize: int = 18,
righttitle: str = None,
Expand All @@ -505,6 +507,12 @@ def set_titles_and_labels(ax: typing.Union[matplotlib.axes.Axes,
maintitlefontsize : int
Text font size for maintitle. A default value of 18 is used if nothing is set.
subtitle: str
Text to use for an optional subtitle.
subtitlefontsize: int
Text font size for subtitle. A default value of 18 is used if nothing is set.
lefttitle : str
Text to use for an optional left-aligned title, if any. For most plots, only a maintitle is enough,
but for some plot types, a lefttitle likely with a right-aligned title, righttitle, can be used together.
Expand Down Expand Up @@ -561,11 +569,17 @@ def set_titles_and_labels(ax: typing.Union[matplotlib.axes.Axes,
"""

if maintitle is not None:
if lefttitle is not None or righttitle is not None:
if subtitle is not None:
fig = ax.get_figure()
fig.suptitle(maintitle, fontsize=maintitlefontsize, y=1.04)
elif lefttitle is not None or righttitle is not None:
ax.set_title(maintitle, fontsize=maintitlefontsize + 2, y=1.12)
else:
ax.set_title(maintitle, fontsize=maintitlefontsize, y=1.04)

if subtitle is not None:
ax.set_title(subtitle, fontsize=subtitlefontsize)

if lefttitle is not None:
ax.set_title(lefttitle, fontsize=lefttitlefontsize, y=1.04, loc='left')

Expand Down

0 comments on commit e9b37d9

Please sign in to comment.