diff --git a/metpy/plots/_util.py b/metpy/plots/_util.py index 48fd108c9e0..7f93e554243 100644 --- a/metpy/plots/_util.py +++ b/metpy/plots/_util.py @@ -6,6 +6,7 @@ from datetime import datetime import posixpath +from matplotlib import patheffects from matplotlib.collections import LineCollection from matplotlib.pyplot import imread import numpy as np @@ -14,7 +15,7 @@ from ..units import concatenate -def add_timestamp(ax, time=None, x=0.99, y=-0.04, ha='right', **kwargs): +def add_timestamp(ax, time=None, x=0.99, y=-0.04, ha='right', high_contrast=False, **kwargs): """Add a timestamp at plot creation time. Adds an ISO format timestamp with the time of plot creation to the plot. @@ -31,6 +32,8 @@ def add_timestamp(ax, time=None, x=0.99, y=-0.04, ha='right', **kwargs): Relative y position on the axes of the timestamp ha : str Horizontal alignment of the time stamp string + high_contrast : bool + Outline text for increased contrast Returns ------- @@ -38,10 +41,22 @@ def add_timestamp(ax, time=None, x=0.99, y=-0.04, ha='right', **kwargs): The `matplotlib.text.Text` instance created """ + if high_contrast: + text_args = {'color': 'white'} + else: + text_args = {} + text_args.update(**kwargs) if not time: time = datetime.utcnow() timestr = datetime.strftime(time, 'Created: %Y-%m-%dT%H:%M:%SZ') - return ax.text(x, y, timestr, ha=ha, transform=ax.transAxes, **kwargs) + text = ax.text(x, y, timestr, ha=ha, transform=ax.transAxes, **text_args) + + if high_contrast: + # Make the text stand out even better using matplotlib's path effects + outline_effect = [patheffects.withStroke(linewidth=2, foreground='black')] + text.set_path_effects(outline_effect) + + return text def _add_logo(fig, x=10, y=25, zorder=100, which='metpy', size='small', **kwargs): diff --git a/metpy/plots/tests/baseline/test_add_timestamp_high_contrast.png b/metpy/plots/tests/baseline/test_add_timestamp_high_contrast.png new file mode 100644 index 00000000000..350c76a7f57 Binary files /dev/null and b/metpy/plots/tests/baseline/test_add_timestamp_high_contrast.png differ diff --git a/metpy/plots/tests/test_util.py b/metpy/plots/tests/test_util.py index 2266fe39828..bc5eceb5eb2 100644 --- a/metpy/plots/tests/test_util.py +++ b/metpy/plots/tests/test_util.py @@ -26,6 +26,16 @@ def test_add_timestamp(): return fig +@pytest.mark.mpl_image_compare(tolerance={'1.4': 9.51}.get(MPL_VERSION, 0.01), + remove_text=True) +def test_add_timestamp_high_contrast(): + """Test adding a timestamp to an axes object.""" + fig = plt.figure(figsize=(9, 9)) + ax = plt.subplot(1, 1, 1) + add_timestamp(ax, time=datetime(2017, 1, 1), high_contrast=True) + return fig + + @pytest.mark.mpl_image_compare(tolerance={'1.4': 0.004}.get(MPL_VERSION, 0.01), remove_text=True) def test_add_metpy_logo_small():