Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support adding tick marks to geographic plots #253

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions proplot/axes/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ def format(
is :rc:`ticklen`.

Minor tick lengths are scaled according
to :rc:`ticklenratio`. Use e.g. ``ax.format(ticklen=1)`` to
to :rc:`tick.lenratio`. Use e.g. ``ax.format(ticklen=1)`` to
set for both axes.
fixticks : bool, optional
Whether to always transform the tick locators to a
Expand Down Expand Up @@ -924,7 +924,7 @@ def format(
if ticklen is not None:
kwticks['size'] = units(ticklen, 'pt')
if which == 'minor':
kwticks['size'] *= rc['ticklenratio']
kwticks['size'] *= rc['tick.lenratio']

# Grid style and toggling
name = 'grid' if which == 'major' else 'gridminor'
Expand Down
34 changes: 26 additions & 8 deletions proplot/axes/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,14 @@ def __init__(self, *args, **kwargs):
proplot.axes.BasemapAxes
"""
super().__init__(*args, **kwargs)
self.tick_params('both', which='both', size=0)

@docstring.add_snippets
def format(
self, *,
lonlim=None, latlim=None, boundinglat=None,
longrid=None, latgrid=None, longridminor=None, latgridminor=None,
tick=None, lontick=None, lattick=None,
lonlocator=None, lonlines=None,
latlocator=None, latlines=None, latmax=None,
lonminorlocator=None, lonminorlines=None,
Expand Down Expand Up @@ -317,12 +319,17 @@ def format(
South Pole-centered projections.
Basemap bounding latitudes must be declared by passing keyword arguments
to `~proplot.constructor.Proj`.
longrid, latgrid : bool, optional
grid, longrid, latgrid : bool, optional
Whether to draw longitude and latitude gridlines.
Default is :rc:`grid`. Use `grid` to toggle both.
longridminor, latgridminor : bool, optional
gridminor, longridminor, latgridminor : bool, optional
Whether to draw "minor" longitude and latitude lines.
Default is :rc:`gridminor`. Use `gridminor` to toggle both.
tick, lontick, lattick : bool or float, optional
*For cartopy axes only*.
Whether to draw longitude and latitude ticks (valid only for rectangular
projections). If ``True``, use the length :rc:`tick.len`. If float, use
this as the tick length. Default is ``False``.
lonlocator, latlocator : str, float, list of float, or \
`~matplotlib.ticker.Locator`, optional
Used to determine the longitude and latitude gridline locations.
Expand Down Expand Up @@ -414,8 +421,7 @@ def format(
*For cartopy axes only.*
The number of interpolation steps used to draw gridlines.
Default is :rc:`grid.nsteps`.
land, ocean, coast, rivers, lakes, borders, innerborders : bool, \
optional
land, ocean, coast, rivers, lakes, borders, innerborders : bool, optional
Toggles various geographic features. These are actually the
:rcraw:`land`, :rcraw:`ocean`, :rcraw:`coast`, :rcraw:`rivers`,
:rcraw:`lakes`, :rcraw:`borders`, and :rcraw:`innerborders`
Expand Down Expand Up @@ -451,6 +457,14 @@ def format(
longridminor = _not_none(longridminor, gridminor)
latgridminor = _not_none(latgridminor, gridminor)

# Tick lengths
for s, itick in zip('xy', (lontick, lattick)):
size = _not_none(itick, tick)
size = rc['tick.len'] if size is True else size
if size is not None:
self.tick_params(s, which='major', size=size)
self.tick_params(s, which='minor', size=size * rc['tick.lenratio'])

# Label toggles
labels = _not_none(labels, rc.get('grid.labels', context=True))
lonlabels = _not_none(lonlabels, labels)
Expand Down Expand Up @@ -780,10 +794,6 @@ def __init__(self, *args, autoextent=None, map_projection=None, **kwargs):
else:
self.set_global()

# Zero out ticks to prevent extra label offset
for axis in (self.xaxis, self.yaxis):
axis.set_tick_params(which='both', size=0)

def _apply_axis_sharing(self): # noqa: U100
"""
No-op for now. In future will hide labels on certain subplots.
Expand Down Expand Up @@ -1027,6 +1037,12 @@ def _update_gridlines(
lonlines = (np.asarray(lonlines) + 180) % 360 - 180 # specific to CartopyAxes
gl.xlocator = mticker.FixedLocator(lonlines)
gl.ylocator = mticker.FixedLocator(latlines)
# TODO: Former works but messes up bounds. Why doesn't latter work?
# self.set_xticks(lonlines, minor=(which == 'minor'))
# self.set_yticks(latlines, minor=(which == 'minor'))
getattr(self.xaxis, 'set_' + which + '_locator')(gl.xlocator)
getattr(self.yaxis, 'set_' + which + '_locator')(gl.ylocator)
self.stale = True

def _update_major_gridlines(
self,
Expand Down Expand Up @@ -1061,6 +1077,8 @@ def _update_major_gridlines(
lataxis = self._lataxis
gl.xformatter = lonaxis.get_major_formatter()
gl.yformatter = lataxis.get_major_formatter()
self.xaxis.set_major_formatter(mticker.NullFormatter())
self.yaxis.set_major_formatter(mticker.NullFormatter())

# Gridline label toggling
# Issue warning instead of error!
Expand Down