Skip to content

Commit

Permalink
Merge pull request #697 from jrleeman/path_effects
Browse files Browse the repository at this point in the history
Add high contrast functionality to timestamp
  • Loading branch information
dopplershift authored Jan 3, 2018
2 parents c179b17 + 5fd2f1c commit 44178e9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
14 changes: 12 additions & 2 deletions metpy/plots/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import posixpath

from matplotlib.collections import LineCollection
import matplotlib.patheffects as mpatheffects
from matplotlib.pyplot import imread
import numpy as np
import pkg_resources

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,26 @@ 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',
'path_effects':
[mpatheffects.withStroke(linewidth=2, foreground='black')]}
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)
return ax.text(x, y, timestr, ha=ha, transform=ax.transAxes, **text_args)


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 44178e9

Please sign in to comment.