Skip to content

Commit

Permalink
Add 'all'/'both'/'neither' options for label sides
Browse files Browse the repository at this point in the history
  • Loading branch information
lukelbd committed Jan 23, 2022
1 parent 0a6e5f5 commit 0f4e03d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
10 changes: 6 additions & 4 deletions docs/projections.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
# <https://scitools.org.uk/cartopy/docs/latest/gallery/lines_and_polygons/always_circular_stereo.html>`__
# from the cartopy website). To disable this feature, set :rcraw:`geo.round` to
# ``False`` or pass ``round=False` to `~proplot.axes.GeoAxes.format`. Please note
# that cartopy cannot add gridline labels to polar plots with circular boundaries.
# that older versions of cartopy cannot add gridlines to maps bounded by circles.
# * To make things more consistent, the `~proplot.constructor.Proj` constructor
# function lets you supply native `PROJ <https://proj.org>`__ keyword names
# for the cartopy `~cartopy.crs.Projection` classes (e.g., `lon0` instead
Expand All @@ -212,15 +212,17 @@
ax2 = fig.subplot(gs[i, 1], proj=proj, backend='basemap') # basemap backend

# Format projections
fig.format(
axs = fig.subplotgrid
axs.format(
land=True,
suptitle='Figure with several projections',
toplabels=('Cartopy examples', 'Basemap examples'),
toplabelweight='normal',
latlines=30, lonlines=60,
lonlabels='b', latlabels='r', # or lonlabels=True, labels=True, etc.
)
fig.subplotgrid[-2:].format(latlines=20, lonlines=30) # dense gridlines for polar plots
axs[:2].format(lonlabels='b', latlabels='r') # or lonlabels=True, lonlabels='bottom',
axs[2:4].format(lonlabels=False, latlabels='both')
axs[4:].format(lonlabels='all', lonlines=30)
pplt.rc.reset()


Expand Down
41 changes: 25 additions & 16 deletions proplot/axes/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,22 @@
which sides of the map. Use the keyword `labels` to set both at once. The
argument must conform to one of the following options:
1. A boolean. ``True`` indicates the left side for latitudes, bottom
side for longitudes, and ``False`` disables all labels.
2. A string or sequence of strings indicating the side names, e.g.
``'left'`` for latitudes or ``('top', 'bottom')`` for longitudes.
3. A string indicating the side names with single characters, e.g.
``'lr'`` for latitudes or ``'bt'`` for longitudes.
4. A boolean 2-tuple indicating whether to draw labels on the
``(left, right)`` sides for latitudes, or ``(bottom, top)``
sides for longitudes.
5. A boolean 4-tuple indicating whether to draw labels on the
``(left, right, bottom, top)`` sides, as with the basemap
`~mpl_toolkits.basemap.Basemap.drawmeridians` and
`~mpl_toolkits.basemap.Basemap.drawparallels` `labels` keyword.
* A boolean. ``True`` indicates the bottom side for longitudes and
the left side for latitudes, and ``False`` disables all labels.
* A string or sequence of strings indicating the side names, e.g.
``'top'`` for longitudes or ``('left', 'right')`` for latitudes.
* A string indicating the side names with single characters, e.g.
``'bt'`` for longitudes or ``'lr'`` for latitudes.
* A string matching ``'neither'`` (no labels), ``'both'`` (equivalent
to ``'bt'`` for longitudes and ``'lr'`` for latitudes), or ``'all'``
(equivalent to ``'lrbt'``, i.e. all sides).
* A boolean 2-tuple indicating whether to draw labels
on the ``(bottom, top)`` sides for longitudes,
and the ``(left, right)`` sides for latitudes.
* A boolean 4-tuple indicating whether to draw labels on the
``(left, right, bottom, top)`` sides, as with the basemap
`~mpl_toolkits.basemap.Basemap.drawmeridians` and
`~mpl_toolkits.basemap.Basemap.drawparallels` `labels` keyword.
loninline, latinline, inlinelabels : bool, default: :rc:`grid.inlinelabels`
*For cartopy axes only.*
Expand Down Expand Up @@ -502,12 +505,18 @@ def _to_label_array(arg, lon=True):
array = [False] * 5
opts = ('left', 'right', 'bottom', 'top', 'geo')
for string in strings:
if string in opts:
if string == 'all':
string = 'lrbt'
elif string == 'both':
string = 'bt' if lon else 'lr'
elif string == 'neither':
string = ''
elif string in opts:
string = string[0]
elif set(string) - set('lrbtg'):
if set(string) - set('lrbtg'):
raise ValueError(
f'Invalid {which}label string {string!r}. Must be one of '
+ ', '.join(map(repr, opts))
+ ', '.join(map(repr, (*opts, 'neither', 'both', 'all')))
+ " or a string of single-letter characters like 'lr'."
)
for char in string:
Expand Down

0 comments on commit 0f4e03d

Please sign in to comment.