Skip to content

Commit

Permalink
Add high contrast functionality to timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
jrleeman committed Jan 2, 2018
1 parent c179b17 commit dab774f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
19 changes: 17 additions & 2 deletions metpy/plots/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -31,17 +32,31 @@ 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
-------
`matplotlib.text.Text`
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):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions metpy/plots/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit dab774f

Please sign in to comment.