Skip to content

Commit 92abfba

Browse files
committed
Cleanup custom_scale example.
- Various docstring cleanups. - The axisbelow hack is not necessary anymore. - Use a normal FuncFormatter instead of a custom Formatter subclass. (We can reuse the same formatter for major and minor ticks.) - No need to make political statements about the Mercator projection.
1 parent cc8b594 commit 92abfba

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

examples/scales/custom_scale.py

+14-25
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,23 @@
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
19+
from matplotlib.ticker import FixedLocator, FuncFormatter
2120
from matplotlib import rcParams
2221

2322

24-
# BUG: this example fails with any other setting of axisbelow
25-
rcParams['axes.axisbelow'] = False
26-
27-
2823
class MercatorLatitudeScale(mscale.ScaleBase):
2924
"""
3025
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.
26+
the system used to scale latitudes in a Mercator__ projection.
3227
3328
The scale function:
3429
ln(tan(y) + sec(y))
@@ -40,8 +35,7 @@ class MercatorLatitudeScale(mscale.ScaleBase):
4035
there is user-defined threshold, above and below which nothing
4136
will be plotted. This defaults to +/- 85 degrees.
4237
43-
source:
44-
http://en.wikipedia.org/wiki/Mercator_projection
38+
__ http://en.wikipedia.org/wiki/Mercator_projection
4539
"""
4640

4741
# The scale class must have a member ``name`` that defines the string used
@@ -77,21 +71,16 @@ def set_default_locators_and_formatters(self, axis):
7771
scale. This is only required if the scale requires custom
7872
locators and formatters. Writing custom locators and
7973
formatters is rather outside the scope of this example, but
80-
there are many helpful examples in ``ticker.py``.
74+
there are many helpful examples in :mod:`.ticker`.
8175
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::
76+
In our case, the Mercator example uses a fixed locator from -90 to 90
77+
degrees and a custom formatter to convert the radians to degrees and
78+
put a degree symbol after the value.
8679
"""
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())
80+
fmt = FuncFormatter(
81+
lambda x, pos=None: f"{np.degrees(x):.0f}\N{DEGREE SIGN}")
82+
axis.set(major_locator=FixedLocator(np.radians(range(-90, 90, 10))),
83+
major_formatter=fmt, minor_formatter=fmt)
9584

9685
def limit_range_for_scale(self, vmin, vmax, minpos):
9786
"""
@@ -176,7 +165,7 @@ def inverted(self):
176165

177166
plt.xlabel('Longitude')
178167
plt.ylabel('Latitude')
179-
plt.title('Mercator: Projection of the Oppressor')
168+
plt.title('Mercator projection')
180169
plt.grid(True)
181170

182171
plt.show()

0 commit comments

Comments
 (0)