7
7
Mercator Projection.
8
8
9
9
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`.
13
12
See the last example in :doc:`/gallery/scales/scales`.
14
13
"""
15
14
16
15
import numpy as np
17
16
from numpy import ma
18
17
from matplotlib import scale as mscale
19
18
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
26
20
27
21
28
22
class MercatorLatitudeScale (mscale .ScaleBase ):
29
23
"""
30
24
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.
32
26
33
27
The scale function:
34
28
ln(tan(y) + sec(y))
@@ -40,8 +34,7 @@ class MercatorLatitudeScale(mscale.ScaleBase):
40
34
there is user-defined threshold, above and below which nothing
41
35
will be plotted. This defaults to +/- 85 degrees.
42
36
43
- source:
44
- http://en.wikipedia.org/wiki/Mercator_projection
37
+ __ http://en.wikipedia.org/wiki/Mercator_projection
45
38
"""
46
39
47
40
# 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):
77
70
scale. This is only required if the scale requires custom
78
71
locators and formatters. Writing custom locators and
79
72
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 `.
81
74
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.
86
78
"""
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 )
95
83
96
84
def limit_range_for_scale (self , vmin , vmax , minpos ):
97
85
"""
@@ -122,25 +110,24 @@ def __init__(self, thresh):
122
110
123
111
def transform_non_affine (self , a ):
124
112
"""
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.
134
121
"""
135
122
masked = ma .masked_where ((a < - self .thresh ) | (a > self .thresh ), a )
136
123
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 )))
138
125
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 )))
140
127
141
128
def inverted (self ):
142
129
"""
143
- Override this method so matplotlib knows how to get the
130
+ Override this method so Matplotlib knows how to get the
144
131
inverse transform for this transform.
145
132
"""
146
133
return MercatorLatitudeScale .InvertedMercatorLatitudeTransform (
@@ -161,7 +148,7 @@ def inverted(self):
161
148
162
149
163
150
# 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.
165
152
mscale .register_scale (MercatorLatitudeScale )
166
153
167
154
@@ -176,7 +163,7 @@ def inverted(self):
176
163
177
164
plt .xlabel ('Longitude' )
178
165
plt .ylabel ('Latitude' )
179
- plt .title ('Mercator: Projection of the Oppressor ' )
166
+ plt .title ('Mercator projection ' )
180
167
plt .grid (True )
181
168
182
169
plt .show ()
0 commit comments