Skip to content

Commit

Permalink
ENH: Adding ability to provide a img_tile found in cartopy.io.img_til…
Browse files Browse the repository at this point in the history
…es. (#728)

* ENH: Adding ability to provide a img_tile found in cartopy.io.img_tiles.

* MNT: Update img_tile_args to be dictionary, fixed baseline.

* FIX: Fix for pytest using classic facecolors. Now will use matplotlib default.

* MNT: Add depreciation warning.

* STY: PEP8 fixes.

* FIX: add_image incorrectly indented.

* FIX: Syntax fix.
  • Loading branch information
zssherman authored Oct 12, 2023
1 parent 5c273b4 commit 573cdaf
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
34 changes: 26 additions & 8 deletions act/plotting/geodisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""

import warnings

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -13,7 +15,7 @@
try:
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.io.img_tiles import Stamen
from cartopy.io import img_tiles

CARTOPY_AVAILABLE = True
except ImportError:
Expand Down Expand Up @@ -56,8 +58,10 @@ def geoplot(
title=None,
projection=None,
plot_buffer=0.08,
stamen='terrain-background',
img_tile=None,
img_tile_args={},
tile=8,
stamen='terrain-background',
cartopy_feature=None,
cmap='rainbow',
text=None,
Expand Down Expand Up @@ -91,11 +95,18 @@ def geoplot(
https://scitools.org.uk/cartopy/docs/latest/reference/projections.html?highlight=projections
plot_buffer : float
Buffer to add around data on plot in lat and lon dimension.
stamen : str
Dataset to use for background image. Set to None to not use
background image.
img_tile : str
Image to use for the plot background. Set to None to not use
background image. For all image background types, see:
https://scitools.org.uk/cartopy/docs/v0.16/cartopy/io/img_tiles.html
Default is None.
img_tile_args : dict
Keyword arguments for the chosen img_tile. These arguments can be
found for the corresponding img_tile here:
https://scitools.org.uk/cartopy/docs/v0.16/cartopy/io/img_tiles.html
Default is an empty dictionary.
tile : int
Tile zoom to use with background image. Higer number indicates
Tile zoom to use with background image. Higher number indicates
more resolution. A value of 8 is typical for a normal sonde plot.
cartopy_feature : list of str or str
Cartopy feature to add to plot.
Expand Down Expand Up @@ -199,9 +210,16 @@ def geoplot(
else:
plt.title(title)

if stamen:
tiler = Stamen(stamen)
if stamen and img_tile is None:
tiler = img_tiles.Stamen(stamen)
ax.add_image(tiler, tile)
warnings.warn(
"Stamen is deprecated in Cartopy and in future versions of ACT, "
"please use img_tile to specify the image background. ")
else:
if img_tile is not None:
tiler = getattr(img_tiles, img_tile)(**img_tile_args)
ax.add_image(tiler, tile)

colorbar_map = None
if cmap is not None:
Expand Down
Binary file modified act/tests/baseline/test_geoplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added act/tests/baseline/test_geoplot_tile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 32 additions & 2 deletions act/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,13 @@ def test_xsection_plot_map():


@pytest.mark.skipif(not CARTOPY_AVAILABLE, reason='Cartopy is not installed.')
@pytest.mark.mpl_image_compare(tolerance=30)
@pytest.mark.mpl_image_compare(style="default", tolerance=30)
def test_geoplot():
sonde_ds = arm.read_netcdf(sample_files.EXAMPLE_SONDE1)
geodisplay = GeographicPlotDisplay({'sgpsondewnpnC1.b1': sonde_ds}, figsize=(15, 8))
try:
geodisplay.geoplot(
'tdry',
marker='.',
cartopy_feature=[
'STATES',
'LAND',
Expand All @@ -430,6 +429,7 @@ def test_geoplot():
'RIVERS',
],
text={'Ponca City': [-97.0725, 36.7125]},
img_tile=None,
stamen=None,
)
try:
Expand All @@ -441,6 +441,36 @@ def test_geoplot():
sonde_ds.close()


@pytest.mark.skipif(not CARTOPY_AVAILABLE, reason='Cartopy is not installed.')
@pytest.mark.mpl_image_compare(style="default", tolerance=30)
def test_geoplot_tile():
sonde_ds = arm.read_netcdf(sample_files.EXAMPLE_SONDE1)
geodisplay = GeographicPlotDisplay({'sgpsondewnpnC1.b1': sonde_ds}, figsize=(15, 8))
try:
geodisplay.geoplot(
'tdry',
cartopy_feature=[
'STATES',
'LAND',
'OCEAN',
'COASTLINE',
'BORDERS',
'LAKES',
'RIVERS',
],
text={'Ponca City': [-97.0725, 36.7125]},
img_tile='GoogleTiles',
img_tile_args={'style': 'street'},
)
try:
return geodisplay.fig
finally:
matplotlib.pyplot.close(geodisplay.fig)
except Exception:
pass
sonde_ds.close()


@pytest.mark.mpl_image_compare(tolerance=30)
def test_stair_graph():
sonde_ds = arm.read_netcdf(sample_files.EXAMPLE_SONDE1)
Expand Down
2 changes: 1 addition & 1 deletion examples/plotting/plot_aaf_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
display = act.plotting.GeographicPlotDisplay(ds, figsize=(12, 10))

# Plot the ARM AAF flight track with respect to Pressure Altitude
display.geoplot('press_alt', lat_field='lat', lon_field='lon', stamen=None)
display.geoplot('press_alt', lat_field='lat', lon_field='lon')

# Display the plot
plt.show()

0 comments on commit 573cdaf

Please sign in to comment.