matplotlib.patches
import
from
matplotlib.path
import
from
matplotlib.projections
import
import
matplotlib.spines
as
mspines
from
matplotlib.ticker
import
,
,
from
matplotlib.transforms
import
,
,
=
class
GeoAxes
(
):
"""
An abstract base class for geographic projections
"""
class
ThetaFormatter
(
):
"""
Used to format the theta tick labels. Converts the native
unit of radians into degrees and adds a degree symbol.
"""
def
__init__
(
self
,
round_to
=
1.0
):
self
.
_round_to
=
round_to
def
__call__
(
self
,
x
,
pos
=
None
):
degrees
=
round
(
(
x
)
/
self
.
_round_to
)
*
self
.
_round_to
return
f
"
{
degrees
:
0.0f
}
\N{DEGREE SIGN}
"
RESOLUTION
=
75
def
_init_axis
(
self
):
self
.
xaxis
=
(
self
)
self
.
yaxis
=
(
self
)
def
clear
(
self
):
super
()
.
clear
()
self
.
set_longitude_grid
(
30
)
self
.
set_latitude_grid
(
15
)
self
.
set_longitude_grid_ends
(
75
)
self
.
xaxis
.
set_minor_locator
(
())
self
.
yaxis
.
set_minor_locator
(
())
self
.
xaxis
.
set_ticks_position
(
'none'
)
self
.
yaxis
.
set_ticks_position
(
'none'
)
self
.
yaxis
.
set_tick_params
(
label1On
=
True
)
self
.
grid
(
[
'axes.grid'
])
(
self
,
-
,
)
(
self
,
-
/
2.0
,
/
2.0
)
def
_set_lim_and_transforms
(
self
):
self
.
transProjection
=
self
.
_get_core_transform
(
self
.
RESOLUTION
)
self
.
transAffine
=
self
.
_get_affine_transform
()
self
.
transAxes
=
(
self
.
bbox
)
self
.
transData
=
self
.
transProjection
+
self
.
transAffine
+
self
.
transAxes
self
.
_xaxis_pretransform
=
()
.
scale
(
1.0
,
self
.
_longitude_cap
*
2.0
)
.
translate
(
0.0
,
-
self
.
_longitude_cap
)
self
.
_xaxis_transform
=
self
.
_xaxis_pretransform
+
self
.
transData
self
.
_xaxis_text1_transform
=
()
.
scale
(
1.0
,
0.0
)
+
self
.
transData
+
()
.
translate
(
0.0
,
4.0
)
self
.
_xaxis_text2_transform
=
()
.
scale
(
1.0
,
0.0
)
+
self
.
transData
+
()
.
translate
(
0.0
,
-
4.0
)
yaxis_stretch
=
()
.
scale
(
*
2
,
1
)
.
translate
(
-
,
0
)
yaxis_space
=
()
.
scale
(
1.0
,
1.1
)
self
.
_yaxis_transform
=
yaxis_stretch
+
self
.
transData
yaxis_text_base
=
yaxis_stretch
+
self
.
transProjection
+
(
yaxis_space
+
self
.
transAffine
+
self
.
transAxes
)
self
.
_yaxis_text1_transform
=
yaxis_text_base
+
()
.
translate
(
-
8.0
,
0.0
)
self
.
_yaxis_text2_transform
=
yaxis_text_base
+
()
.
translate
(
8.0
,
0.0
)
def
_get_affine_transform
(
self
):
transform
=
self
.
_get_core_transform
(
1
)
xscale
,
_
=
transform
.
transform
((
,
0
))
_
,
yscale
=
transform
.
transform
((
0
,
/
2
))
return
()
.
scale
(
0.5
/
xscale
,
0.5
/
yscale
)
.
translate
(
0.5
,
0.5
)
def
get_xaxis_transform
(
self
,
which
=
'grid'
):
"""
Override this method to provide a transformation for the
x-axis tick labels.
Returns a tuple of the form (transform, valign, halign)
"""
if
which
not
in
[
'tick1'
,
'tick2'
,
'grid'
]:
raise
ValueError
(
"'which' must be one of 'tick1', 'tick2', or 'grid'"
)
return
self
.
_xaxis_transform
def
get_xaxis_text1_transform
(
self
,
pad
):
return
self
.
_xaxis_text1_transform
,
'bottom'
,
'center'
def
get_xaxis_text2_transform
(
self
,
pad
):
"""
Override this method to provide a transformation for the
secondary x-axis tick labels.
Returns a tuple of the form (transform, valign, halign)
"""
return
self
.
_xaxis_text2_transform
,
'top'
,
'center'
def
get_yaxis_transform
(
self
,
which
=
'grid'
):
"""
Override this method to provide a transformation for the
y-axis grid and ticks.
"""
if
which
not
in
[
'tick1'
,
'tick2'
,
'grid'
]:
raise
ValueError
(
"'which' must be one of 'tick1', 'tick2', or 'grid'"
)
return
self
.
_yaxis_transform
def
get_yaxis_text1_transform
(
self
,
pad
):
"""
Override this method to provide a transformation for the
y-axis tick labels.
Returns a tuple of the form (transform, valign, halign)
"""
return
self
.
_yaxis_text1_transform
,
'center'
,
'right'
def
get_yaxis_text2_transform
(
self
,
pad
):
"""
Override this method to provide a transformation for the
secondary y-axis tick labels.
Returns a tuple of the form (transform, valign, halign)
"""
return
self
.
_yaxis_text2_transform
,
'center'
,
'left'
def
_gen_axes_patch
(
self
):
"""
Override this method to define the shape that is used for the
background of the plot. It should be a subclass of Patch.
In this case, it is a Circle (that may be warped by the Axes
transform into an ellipse). Any data and gridlines will be
clipped to this shape.
"""
return
((
0.5
,
0.5
),
0.5
)
def
_gen_axes_spines
(
self
):
return
{
'geo'
:
(
self
,
(
0.5
,
0.5
),
0.5
)}
def
set_yscale
(
self
,
*
args
,
**
kwargs
):
if
args
[
0
]
!=
'linear'
:
raise
NotImplementedError
set_xscale
=
set_yscale
def
set_xlim
(
self
,
*
args
,
**
kwargs
):
raise
TypeError
(
"Changing axes limits of a geographic projection is "
"not supported. Please consider using Cartopy."
)
set_ylim
=
set_xlim
def
format_coord
(
self
,
lon
,
lat
):
"""
Override this method to change how the values are displayed in
the status bar.
In this case, we want them to be displayed in degrees N/S/E/W.
"""
lon
,
lat
=
([
lon
,
lat
])
ns
=
'N'
if
lat
>=
0.0
else
'S'
ew
=
'E'
if
lon
>=
0.0
else
'W'
return
(
'
%f
\N{DEGREE SIGN}
%s
,
%f
\N{DEGREE SIGN}
%s
'
%
(
abs
(
lat
),
ns
,
abs
(
lon
),
ew
))
def
set_longitude_grid
(
self
,
degrees
):
"""
Set the number of degrees between each longitude grid.
This is an example method that is specific to this projection
class -- it provides a more convenient interface to set the
ticking than set_xticks would.
"""
grid
=
(
-
180
+
degrees
,
180
,
degrees
)
self
.
xaxis
.
set_major_locator
(
(
(
grid
)))
self
.
xaxis
.
set_major_formatter
(
self
.
ThetaFormatter
(
degrees
))
def
set_latitude_grid
(
self
,
degrees
):
"""
Set the number of degrees between each longitude grid.
This is an example method that is specific to this projection
class -- it provides a more convenient interface than
set_yticks would.
"""
grid
=
(
-
90
+
degrees
,
90
,
degrees
)
self
.
yaxis
.
set_major_locator
(
(
(
grid
)))
self
.
yaxis
.
set_major_formatter
(
self
.
ThetaFormatter
(
degrees
))
def
set_longitude_grid_ends
(
self
,
degrees
):
"""
Set the latitude(s) at which to stop drawing the longitude grids.
Often, in geographic projections, you wouldn't want to draw
longitude gridlines near the poles. This allows the user to
specify the degree at which to stop drawing longitude grids.
This is an example method that is specific to this projection
class -- it provides an interface to something that has no
analogy in the base Axes class.
"""
self
.
_longitude_cap
=
(
degrees
)
self
.
_xaxis_pretransform
.
clear
()
.
scale
(
1.0
,
self
.
_longitude_cap
*
2.0
)
.
translate
(
0.0
,
-
self
.
_longitude_cap
)
def
get_data_ratio
(
self
):
"""
Return the aspect ratio of the data itself.
This method should be overridden by any Axes that have a
fixed data ratio.
"""
return
1.0
def
can_zoom
(
self
):
"""
Return whether this Axes supports the zoom box button functionality.
This Axes object does not support interactive zoom box.
"""
return
False
def
can_pan
(
self
):
"""
Return whether this Axes supports the pan/zoom button functionality.
This Axes object does not support interactive pan/zoom.
"""
return
False
def
start_pan
(
self
,
x
,
y
,
button
):
pass
def
end_pan
(
self
):
pass
def
drag_pan
(
self
,
button
,
key
,
x
,
y
):
pass
class
HammerAxes
(
):
"""
A custom class for the Aitoff-Hammer projection, an equal-area map
projection.
https://en.wikipedia.org/wiki/Hammer_projection
"""
name
=
'custom_hammer'
class
HammerTransform
(
):
"""The base Hammer transform."""
input_dims
=
output_dims
=
2
def
__init__
(
self
,
resolution
):
"""
Create a new Hammer transform. Resolution is the number of steps
to interpolate between each input line segment to approximate its
path in curved Hammer space.
"""
.
__init__
(
self
)
self
.
_resolution
=
resolution
def
transform_non_affine
(
self
,
ll
):
longitude
,
latitude
=
ll
.
T
half_long
=
longitude
/
2
cos_latitude
=
(
latitude
)
sqrt2
=
(
2
)
alpha
=
(
1
+
cos_latitude
*
(
half_long
))
x
=
(
2
*
sqrt2
)
*
(
cos_latitude
*
(
half_long
))
/
alpha
y
=
(
sqrt2
*
(
latitude
))
/
alpha
return
([
x
,
y
])
def
transform_path_non_affine
(
self
,
path
):
ipath
=
path
.
interpolated
(
self
.
_resolution
)
return
(
self
.
transform
(
ipath
.
vertices
),
ipath
.
codes
)
def
inverted
(
self
):
return
(
self
.
_resolution
)
class
InvertedHammerTransform
(
):
input_dims
=
output_dims
=
2
def
__init__
(
self
,
resolution
):
.
__init__
(
self
)
self
.
_resolution
=
resolution
def
transform_non_affine
(
self
,
xy
):
x
,
y
=
xy
.
T
z
=
(
1
-
(
x
/
4
)
**
2
-
(
y
/
2
)
**
2
)
longitude
=
2
*
((
z
*
x
)
/
(
2
*
(
2
*
z
**
2
-
1
)))
latitude
=
(
y
*
z
)
return
([
longitude
,
latitude
])
def
inverted
(
self
):
return
(
self
.
_resolution
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
_longitude_cap
=
/
2.0
super
()
.
__init__
(
*
args
,
**
kwargs
)
self
.
set_aspect
(
0.5
,
adjustable
=
'box'
,
anchor
=
'C'
)
self
.
clear
()
def
_get_core_transform
(
self
,
resolution
):
return
self
.
HammerTransform
(
resolution
)
(
)
if
__name__
==
'__main__'
:
import
matplotlib.pyplot
as
plt
,
ax
=
(
subplot_kw
=
{
'projection'
:
'custom_hammer'
})
([
-
1
,
1
,
1
],
[
-
1
,
-
1
,
1
],
"o-"
)
()
()