Skip to content

Commit a860d08

Browse files
authored
missing region border in plots (#110)
fixes #109: missing region border - added kwargs to plot_basemap to apply 'fast' projection and central latitude - added 'fast' as an option to the plot_args of plot_spatial_dataset and plot_catalog - updated docstrings - simplified calculation for the aspect scaling - added missing else statement to handle set_global flag in plot_basemap
1 parent fa789f0 commit a860d08

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

csep/utils/plots.py

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,8 @@ def get_hist(x, mws, normed=True):
554554
pyplot.show()
555555

556556
def plot_basemap(basemap, extent, ax=None, coastline=True, borders=False, linecolor='black', linewidth=True,
557-
grid=False, grid_labels=False, set_global=False, show=False):
557+
grid=False, grid_labels=False, set_global=False, show=False, projection=ccrs.PlateCarree(), apprx=False,
558+
central_latitude=0.0):
558559
""" Wrapper function for multiple cartopy base plots, including access to standard raster webservices
559560
560561
Args:
@@ -568,16 +569,29 @@ def plot_basemap(basemap, extent, ax=None, coastline=True, borders=False, linec
568569
linecolor (str): Color of borders and coast lines. default 'black',
569570
grid (bool): Draws a grid in the basemap
570571
grid_labels (bool): Annotate grid values
572+
apprx (bool): If true, approximates transformation by setting aspect ratio of axes based on middle latitude
573+
central_latitude (float): average latitude from plotting region
571574
572575
Returns:
573576
:class:`matplotlib.pyplot.ax` object
574577
575578
"""
576579
if ax is None:
577-
fig = pyplot.figure()
578-
ax = fig.add_subplot(111, projection=ccrs.PlateCarree(central_longitude=0.0))
580+
if apprx:
581+
projection = ccrs.PlateCarree()
582+
fig = pyplot.figure()
583+
ax = fig.add_subplot(111, projection=projection)
584+
# Re-aspect plot (only for plain matplotlib plots, or when using PlateCarree)
585+
LATKM = 110.574
586+
ax.set_aspect(111.320 * numpy.cos(numpy.deg2rad(central_latitude)) / LATKM)
587+
else:
588+
fig = pyplot.figure()
589+
ax = fig.add_subplot(111, projection=projection)
590+
579591
if set_global:
580592
ax.set_global()
593+
else:
594+
ax.set_extent(extents=extent, crs=ccrs.PlateCarree())
581595

582596
try:
583597
# Set adaptive scaling
@@ -613,7 +627,6 @@ def plot_basemap(basemap, extent, ax=None, coastline=True, borders=False, linec
613627

614628
return ax
615629

616-
617630
def plot_catalog(catalog, ax=None, show=False, extent=None, set_global=False, plot_args=None):
618631
""" Plot catalog in a region
619632
@@ -629,7 +642,8 @@ def plot_catalog(catalog, ax=None, show=False, extent=None, set_global=False, pl
629642
- :title: :class:`str` - default :class:`catalog.name`
630643
- :title_size: :class:`int` - default 10
631644
- :filename: :class:`str` - File to save figure. default None
632-
- :projection: :class:`cartopy.crs.Projection` - default :class:`cartopy.crs.PlateCarree`
645+
- :projection: :class:`cartopy.crs.Projection` - default :class:`cartopy.crs.PlateCarree`. Note: this can be
646+
'fast' to apply an approximate transformation of axes.
633647
- :grid: :class:`bool` - default True
634648
- :grid_labels: :class:`bool` - default True
635649
- :marker: :class:`str` - Marker type
@@ -689,22 +703,32 @@ def plot_catalog(catalog, ax=None, show=False, extent=None, set_global=False, pl
689703
pass
690704

691705
if extent is None:
692-
dh = (bbox[1] - bbox[0])/20.
706+
dh = (bbox[1] - bbox[0]) / 20.
693707
dv = (bbox[3] - bbox[2]) / 20.
694708
extent = [bbox[0] - dh, bbox[1]+dh, bbox[2] -dv, bbox[3] + dv]
695709

710+
apprx = False
711+
central_latitude = 0.0
712+
if projection == 'fast':
713+
projection = ccrs.PlateCarree()
714+
apprx = True
715+
n_lats = len(catalog.region.ys) // 2
716+
central_latitude = catalog.region.ys[n_lats]
717+
696718
# Instantiage GeoAxes object
697719
if ax is None:
698720
fig = pyplot.figure(figsize=figsize)
699721
ax = fig.add_subplot(111, projection=projection)
700-
if set_global:
701-
ax.set_global()
702-
else:
703-
ax.set_extent(extents=extent, crs=ccrs.PlateCarree()) # Defined extent always in lat/lon
722+
723+
if set_global:
724+
ax.set_global()
725+
else:
726+
ax.set_extent(extents=extent, crs=ccrs.PlateCarree()) # Defined extent always in lat/lon
704727

705728
# Basemap plotting
706729
ax = plot_basemap(basemap, extent, ax=ax, coastline=coastline, borders=borders,
707-
linecolor=linecolor, linewidth=linewidth)
730+
linecolor=linecolor, linewidth=linewidth, projection=projection, apprx=apprx,
731+
central_latitude=central_latitude)
708732

709733
# Scaling function
710734
mw_range = [min(catalog.get_magnitudes()), max(catalog.get_magnitudes())]
@@ -758,7 +782,6 @@ def size_map(markersize, values, scale):
758782

759783
return ax
760784

761-
762785
def plot_spatial_dataset(gridded, region, ax=None, show=False, extent=None, set_global=False, plot_args=None):
763786
""" Plot spatial dataset such as data from a gridded forecast
764787
@@ -823,6 +846,13 @@ def plot_spatial_dataset(gridded, region, ax=None, show=False, extent=None, set_
823846
alpha = plot_args.get('alpha', 1)
824847
alpha_exp = plot_args.get('alpha_exp', 0)
825848

849+
apprx = False
850+
central_latitude = 0.0
851+
if projection == 'fast':
852+
projection = ccrs.PlateCarree()
853+
apprx = True
854+
n_lats = len(region.ys) // 2
855+
central_latitude = region.ys[n_lats]
826856

827857
# Instantiage GeoAxes object
828858
if ax is None:
@@ -838,7 +868,8 @@ def plot_spatial_dataset(gridded, region, ax=None, show=False, extent=None, set_
838868

839869
# Basemap plotting
840870
ax = plot_basemap(basemap, extent, ax=ax, coastline=coastline, borders=borders,
841-
linecolor=linecolor, linewidth=linewidth)
871+
linecolor=linecolor, linewidth=linewidth, projection=projection, apprx=apprx,
872+
central_latitude=central_latitude)
842873

843874
## Define colormap and transparency function
844875
if isinstance(cmap, str) or not cmap:
@@ -874,7 +905,7 @@ def plot_spatial_dataset(gridded, region, ax=None, show=False, extent=None, set_
874905

875906
if region_border:
876907
pts = region.tight_bbox()
877-
ax.plot(pts[:,0], pts[:,1], lw=1, color='black')
908+
ax.plot(pts[:,0], pts[:,1], lw=1, color='black', transform=ccrs.PlateCarree())
878909

879910
# matplotlib figure options
880911
ax.set_title(title, y=1.06)

0 commit comments

Comments
 (0)