Skip to content

Commit

Permalink
MNT: Update ticks for SkewT (Fixes Unidata#987)
Browse files Browse the repository at this point in the history
This updates the implementation for ticks in SkewT based on changes in
matplotlib. This greatly cleans this up, and for some reason, is
necessary with matplotlib 3.1.
  • Loading branch information
dopplershift authored and mgrover1 committed Jul 15, 2019
1 parent 1696b30 commit f922e43
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 65 deletions.
97 changes: 32 additions & 65 deletions metpy/plots/skewt.py
Expand Up @@ -7,6 +7,11 @@
`SkewT`, as well as a class for making a `Hodograph`.
"""

try:
from contextlib import ExitStack
except ImportError:
from contextlib2 import ExitStack

import matplotlib
from matplotlib.axes import Axes
import matplotlib.axis as maxis
Expand Down Expand Up @@ -37,69 +42,31 @@ class SkewXTick(maxis.XTick):
and draw as appropriate. It also performs similar checking for gridlines.
"""

def update_position(self, loc):
"""Set the location of tick in data coords with scalar *loc*."""
# This ensures that the new value of the location is set before
# any other updates take place.
self._loc = loc
super(SkewXTick, self).update_position(loc)

def _has_default_loc(self):
return self.get_loc() is None

def _need_lower(self):
return (self._has_default_loc()
or transforms.interval_contains(self.axes.lower_xlim, self.get_loc()))

def _need_upper(self):
return (self._has_default_loc()
or transforms.interval_contains(self.axes.upper_xlim, self.get_loc()))

@property
def gridOn(self): # noqa: N802
"""Control whether the gridline is drawn for this tick."""
return (self._gridOn and (self._has_default_loc()
or transforms.interval_contains(self.get_view_interval(), self.get_loc())))

@gridOn.setter
def gridOn(self, value): # noqa: N802
self._gridOn = value

@property
def tick1On(self): # noqa: N802
"""Control whether the lower tick mark is drawn for this tick."""
return self._tick1On and self._need_lower()

@tick1On.setter
def tick1On(self, value): # noqa: N802
self._tick1On = value

@property
def label1On(self): # noqa: N802
"""Control whether the lower tick label is drawn for this tick."""
return self._label1On and self._need_lower()

@label1On.setter
def label1On(self, value): # noqa: N802
self._label1On = value

@property
def tick2On(self): # noqa: N802
"""Control whether the upper tick mark is drawn for this tick."""
return self._tick2On and self._need_upper()

@tick2On.setter
def tick2On(self, value): # noqa: N802
self._tick2On = value

@property
def label2On(self): # noqa: N802
"""Control whether the upper tick label is drawn for this tick."""
return self._label2On and self._need_upper()

@label2On.setter
def label2On(self, value): # noqa: N802
self._label2On = value
# Taken from matplotlib's SkewT example to update for matplotlib 3.1's changes to
# state management for ticks. See matplotlib/matplotlib#10088
def draw(self, renderer):
"""Draw the tick."""
# When adding the callbacks with `stack.callback`, we fetch the current
# visibility state of the artist with `get_visible`; the ExitStack will
# restore these states (`set_visible`) at the end of the block (after
# the draw).
with ExitStack() as stack:
for artist in [self.gridline, self.tick1line, self.tick2line,
self.label1, self.label2]:
stack.callback(artist.set_visible, artist.get_visible())
needs_lower = transforms.interval_contains(
self.axes.lower_xlim, self.get_loc())
needs_upper = transforms.interval_contains(
self.axes.upper_xlim, self.get_loc())
self.tick1line.set_visible(
self.tick1line.get_visible() and needs_lower)
self.label1.set_visible(
self.label1.get_visible() and needs_lower)
self.tick2line.set_visible(
self.tick2line.get_visible() and needs_upper)
self.label2.set_visible(
self.label2.get_visible() and needs_upper)
super(SkewXTick, self).draw(renderer)

def get_view_interval(self):
"""Get the view interval."""
Expand Down Expand Up @@ -168,7 +135,7 @@ def __init__(self, *args, **kwargs):
"""
# This needs to be popped and set before moving on
self.rot = kwargs.pop('rotation', 30)
Axes.__init__(self, *args, **kwargs)
super(Axes, self).__init__(*args, **kwargs)

def _init_axis(self):
# Taken from Axes and modified to use our modified X-axis
Expand All @@ -195,7 +162,7 @@ def _set_lim_and_transforms(self):
"""
# Get the standard transform setup from the Axes base class
Axes._set_lim_and_transforms(self)
super(Axes, self)._set_lim_and_transforms()

# Need to put the skew in the middle, after the scale and limits,
# but before the transAxes. This way, the skew is done in Axes
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -52,6 +52,7 @@
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*',
install_requires=['matplotlib>=2.0.0', 'numpy>=1.12.0', 'scipy>=0.17.0',
'pint!=0.9', 'xarray>=0.10.7', 'enum34;python_version<"3.4"',
'contextlib2;python_version<"3.6"',
'pooch>=0.1, <0.3', 'traitlets>=4.3.0'],
extras_require={
'dev': ['ipython[all]>=3.1'],
Expand Down

0 comments on commit f922e43

Please sign in to comment.