Skip to content

Commit 569e12e

Browse files
authored
Merge pull request #16240 from anntzer/scalex
DOC: Cleanup custom_scale example.
2 parents f9bf67a + 463614d commit 569e12e

File tree

1 file changed

+26
-39
lines changed

1 file changed

+26
-39
lines changed

examples/scales/custom_scale.py

+26-39
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,22 @@
77
Mercator Projection.
88
99
Unless you are making special use of the `~.Transform` class, you probably
10-
don't need to use this verbose method, and instead can use
11-
`~.matplotlib.scale.FuncScale` and the ``'function'`` option of
12-
`~.matplotlib.axes.Axes.set_xscale` and `~.matplotlib.axes.Axes.set_yscale`.
10+
don't need to use this verbose method, and instead can use `~.scale.FuncScale`
11+
and the ``'function'`` option of `~.Axes.set_xscale` and `~.Axes.set_yscale`.
1312
See the last example in :doc:`/gallery/scales/scales`.
1413
"""
1514

1615
import numpy as np
1716
from numpy import ma
1817
from matplotlib import scale as mscale
1918
from matplotlib import transforms as mtransforms
20-
from matplotlib.ticker import Formatter, FixedLocator
21-
from matplotlib import rcParams
22-
23-
24-
# BUG: this example fails with any other setting of axisbelow
25-
rcParams['axes.axisbelow'] = False
19+
from matplotlib.ticker import FixedLocator, FuncFormatter
2620

2721

2822
class MercatorLatitudeScale(mscale.ScaleBase):
2923
"""
3024
Scales data in range -pi/2 to pi/2 (-90 to 90 degrees) using
31-
the system used to scale latitudes in a Mercator projection.
25+
the system used to scale latitudes in a Mercator__ projection.
3226
3327
The scale function:
3428
ln(tan(y) + sec(y))
@@ -40,8 +34,7 @@ class MercatorLatitudeScale(mscale.ScaleBase):
4034
there is user-defined threshold, above and below which nothing
4135
will be plotted. This defaults to +/- 85 degrees.
4236
43-
source:
44-
http://en.wikipedia.org/wiki/Mercator_projection
37+
__ http://en.wikipedia.org/wiki/Mercator_projection
4538
"""
4639

4740
# The scale class must have a member ``name`` that defines the string used
@@ -77,21 +70,16 @@ def set_default_locators_and_formatters(self, axis):
7770
scale. This is only required if the scale requires custom
7871
locators and formatters. Writing custom locators and
7972
formatters is rather outside the scope of this example, but
80-
there are many helpful examples in ``ticker.py``.
73+
there are many helpful examples in :mod:`.ticker`.
8174
82-
In our case, the Mercator example uses a fixed locator from
83-
-90 to 90 degrees and a custom formatter class to put convert
84-
the radians to degrees and put a degree symbol after the
85-
value::
75+
In our case, the Mercator example uses a fixed locator from -90 to 90
76+
degrees and a custom formatter to convert the radians to degrees and
77+
put a degree symbol after the value.
8678
"""
87-
class DegreeFormatter(Formatter):
88-
def __call__(self, x, pos=None):
89-
return "%d\N{DEGREE SIGN}" % np.degrees(x)
90-
91-
axis.set_major_locator(FixedLocator(
92-
np.radians(np.arange(-90, 90, 10))))
93-
axis.set_major_formatter(DegreeFormatter())
94-
axis.set_minor_formatter(DegreeFormatter())
79+
fmt = FuncFormatter(
80+
lambda x, pos=None: f"{np.degrees(x):.0f}\N{DEGREE SIGN}")
81+
axis.set(major_locator=FixedLocator(np.radians(range(-90, 90, 10))),
82+
major_formatter=fmt, minor_formatter=fmt)
9583

9684
def limit_range_for_scale(self, vmin, vmax, minpos):
9785
"""
@@ -122,25 +110,24 @@ def __init__(self, thresh):
122110

123111
def transform_non_affine(self, a):
124112
"""
125-
This transform takes an Nx1 ``numpy`` array and returns a
126-
transformed copy. Since the range of the Mercator scale
127-
is limited by the user-specified threshold, the input
128-
array must be masked to contain only valid values.
129-
``matplotlib`` will handle masked arrays and remove the
130-
out-of-range data from the plot. Importantly, the
131-
``transform`` method *must* return an array that is the
132-
same shape as the input array, since these values need to
133-
remain synchronized with values in the other dimension.
113+
This transform takes a numpy array and returns a transformed copy.
114+
Since the range of the Mercator scale is limited by the
115+
user-specified threshold, the input array must be masked to
116+
contain only valid values. Matplotlib will handle masked arrays
117+
and remove the out-of-range data from the plot. However, the
118+
returned array *must* have the same shape as the input array, since
119+
these values need to remain synchronized with values in the other
120+
dimension.
134121
"""
135122
masked = ma.masked_where((a < -self.thresh) | (a > self.thresh), a)
136123
if masked.mask.any():
137-
return ma.log(np.abs(ma.tan(masked) + 1.0 / ma.cos(masked)))
124+
return ma.log(np.abs(ma.tan(masked) + 1 / ma.cos(masked)))
138125
else:
139-
return np.log(np.abs(np.tan(a) + 1.0 / np.cos(a)))
126+
return np.log(np.abs(np.tan(a) + 1 / np.cos(a)))
140127

141128
def inverted(self):
142129
"""
143-
Override this method so matplotlib knows how to get the
130+
Override this method so Matplotlib knows how to get the
144131
inverse transform for this transform.
145132
"""
146133
return MercatorLatitudeScale.InvertedMercatorLatitudeTransform(
@@ -161,7 +148,7 @@ def inverted(self):
161148

162149

163150
# Now that the Scale class has been defined, it must be registered so
164-
# that ``matplotlib`` can find it.
151+
# that Matplotlib can find it.
165152
mscale.register_scale(MercatorLatitudeScale)
166153

167154

@@ -176,7 +163,7 @@ def inverted(self):
176163

177164
plt.xlabel('Longitude')
178165
plt.ylabel('Latitude')
179-
plt.title('Mercator: Projection of the Oppressor')
166+
plt.title('Mercator projection')
180167
plt.grid(True)
181168

182169
plt.show()

0 commit comments

Comments
 (0)