Skip to content

Commit da9e5d5

Browse files
committed
BUG: Fix drawing gridlines in skew-T example (Fixes #6873)
For some reason, `_adjust_location()` on the `SkewSpine` was not being called; this resulted in `SkewXAxis` having the default (and out of date) bounds for the upper x-axis. Not sure what the root cause is, but refactoring this so that it isn't relying on the Spine to modify attributes on the XAxis both fixes the problem and cleans up the design greatly.
1 parent 59c9f36 commit da9e5d5

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

examples/api/skewt.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
import matplotlib.transforms as transforms
1919
import matplotlib.axis as maxis
2020
import matplotlib.spines as mspines
21-
import matplotlib.path as mpath
2221
from matplotlib.projections import register_projection
2322

23+
2424
# The sole purpose of this class is to look at the upper, lower, or total
2525
# interval as appropriate and see what parts of the tick to draw, if any.
26-
27-
2826
class SkewXTick(maxis.XTick):
2927
def draw(self, renderer):
3028
if not self.get_visible():
@@ -56,38 +54,31 @@ def draw(self, renderer):
5654
# This class exists to provide two separate sets of intervals to the tick,
5755
# as well as create instances of the custom tick
5856
class SkewXAxis(maxis.XAxis):
59-
def __init__(self, *args, **kwargs):
60-
maxis.XAxis.__init__(self, *args, **kwargs)
61-
self.upper_interval = 0.0, 1.0
62-
6357
def _get_tick(self, major):
6458
return SkewXTick(self.axes, 0, '', major=major)
6559

6660
@property
6761
def lower_interval(self):
6862
return self.axes.viewLim.intervalx
6963

64+
@property
65+
def upper_interval(self):
66+
return self.axes.upper_xlim
67+
7068
def get_view_interval(self):
71-
return self.upper_interval[0], self.axes.viewLim.intervalx[1]
69+
return self.upper_interval[0], self.lower_interval[1]
7270

7371

7472
# This class exists to calculate the separate data range of the
7573
# upper X-axis and draw the spine there. It also provides this range
7674
# to the X-axis artist for ticking and gridlines
7775
class SkewSpine(mspines.Spine):
7876
def _adjust_location(self):
79-
trans = self.axes.transDataToAxes.inverted()
77+
pts = self._path.vertices
8078
if self.spine_type == 'top':
81-
yloc = 1.0
79+
pts[:, 0] = self.axis.upper_interval
8280
else:
83-
yloc = 0.0
84-
left = trans.transform_point((0.0, yloc))[0]
85-
right = trans.transform_point((1.0, yloc))[0]
86-
87-
pts = self._path.vertices
88-
pts[0, 0] = left
89-
pts[1, 0] = right
90-
self.axis.upper_interval = (left, right)
81+
pts[:, 0] = self.axis.lower_interval
9182

9283

9384
# This class handles registration of the skew-xaxes as a projection as well
@@ -143,6 +134,12 @@ def _set_lim_and_transforms(self):
143134
transforms.IdentityTransform()) +
144135
transforms.Affine2D().skew_deg(rot, 0)) + self.transAxes
145136

137+
@property
138+
def upper_xlim(self):
139+
pts = [[0., 1.], [1., 1.]]
140+
return self.transDataToAxes.inverted().transform(pts)[:, 0]
141+
142+
146143
# Now register the projection with matplotlib so the user can select
147144
# it.
148145
register_projection(SkewXAxes)
@@ -242,7 +239,7 @@ def _set_lim_and_transforms(self):
242239
plt.grid(True)
243240

244241
# Plot the data using normal plotting functions, in this case using
245-
# log scaling in Y, as dicatated by the typical meteorological plot
242+
# log scaling in Y, as dictated by the typical meteorological plot
246243
ax.semilogy(T, p)
247244
ax.semilogy(Td, p)
248245

0 commit comments

Comments
 (0)