FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib/examples/api/custom_scale_example.py at master · dpsanders/matplotlib · GitHub
dpsanders
matplotlib
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib
/
examples
/
api
/
custom_scale_example.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
173 lines (140 loc) · 6.25 KB
Breadcrumbs
matplotlib
/
examples
/
api
/
custom_scale_example.py
Copy path
File metadata and controls
173 lines (140 loc) · 6.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from
__future__
import
unicode_literals
import
numpy
as
np
from
numpy
import
ma
from
matplotlib
import
scale
as
mscale
from
matplotlib
import
transforms
as
mtransforms
from
matplotlib
.
ticker
import
Formatter
,
FixedLocator
class
MercatorLatitudeScale
(
mscale
.
ScaleBase
):
"""
Scales data in range -pi/2 to pi/2 (-90 to 90 degrees) using
the system used to scale latitudes in a Mercator projection.
The scale function:
ln(tan(y) + sec(y))
The inverse scale function:
atan(sinh(y))
Since the Mercator scale tends to infinity at +/- 90 degrees,
there is user-defined threshold, above and below which nothing
will be plotted. This defaults to +/- 85 degrees.
source:
http://en.wikipedia.org/wiki/Mercator_projection
"""
# The scale class must have a member ``name`` that defines the
# string used to select the scale. For example,
# ``gca().set_yscale("mercator")`` would be used to select this
# scale.
name
=
'mercator'
def
__init__
(
self
,
axis
,
**
kwargs
):
"""
Any keyword arguments passed to ``set_xscale`` and
``set_yscale`` will be passed along to the scale's
constructor.
thresh: The degree above which to crop the data.
"""
mscale
.
ScaleBase
.
__init__
(
self
)
thresh
=
kwargs
.
pop
(
"thresh"
, (
85
/
180.0
)
*
np
.
pi
)
if
thresh
>=
np
.
pi
/
2.0
:
raise
ValueError
(
"thresh must be less than pi/2"
)
self
.
thresh
=
thresh
def
get_transform
(
self
):
"""
Override this method to return a new instance that does the
actual transformation of the data.
The MercatorLatitudeTransform class is defined below as a
nested class of this one.
"""
return
self
.
MercatorLatitudeTransform
(
self
.
thresh
)
def
set_default_locators_and_formatters
(
self
,
axis
):
"""
Override to set up the locators and formatters to use with the
scale. This is only required if the scale requires custom
locators and formatters. Writing custom locators and
formatters is rather outside the scope of this example, but
there are many helpful examples in ``ticker.py``.
In our case, the Mercator example uses a fixed locator from
-90 to 90 degrees and a custom formatter class to put convert
the radians to degrees and put a degree symbol after the
value::
"""
class
DegreeFormatter
(
Formatter
):
def
__call__
(
self
,
x
,
pos
=
None
):
# \u00b0 : degree symbol
return
"%d
\u00b0
"
%
((
x
/
np
.
pi
)
*
180.0
)
deg2rad
=
np
.
pi
/
180.0
axis
.
set_major_locator
(
FixedLocator
(
np
.
arange
(
-
90
,
90
,
10
)
*
deg2rad
))
axis
.
set_major_formatter
(
DegreeFormatter
())
axis
.
set_minor_formatter
(
DegreeFormatter
())
def
limit_range_for_scale
(
self
,
vmin
,
vmax
,
minpos
):
"""
Override to limit the bounds of the axis to the domain of the
transform. In the case of Mercator, the bounds should be
limited to the threshold that was passed in. Unlike the
autoscaling provided by the tick locators, this range limiting
will always be adhered to, whether the axis range is set
manually, determined automatically or changed through panning
and zooming.
"""
return
max
(
vmin
,
-
self
.
thresh
),
min
(
vmax
,
self
.
thresh
)
class
MercatorLatitudeTransform
(
mtransforms
.
Transform
):
# There are two value members that must be defined.
# ``input_dims`` and ``output_dims`` specify number of input
# dimensions and output dimensions to the transformation.
# These are used by the transformation framework to do some
# error checking and prevent incompatible transformations from
# being connected together. When defining transforms for a
# scale, which are, by definition, separable and have only one
# dimension, these members should always be set to 1.
input_dims
=
1
output_dims
=
1
is_separable
=
True
def
__init__
(
self
,
thresh
):
mtransforms
.
Transform
.
__init__
(
self
)
self
.
thresh
=
thresh
def
transform_non_affine
(
self
,
a
):
"""
This transform takes an Nx1 ``numpy`` array and returns a
transformed copy. Since the range of the Mercator scale
is limited by the user-specified threshold, the input
array must be masked to contain only valid values.
``matplotlib`` will handle masked arrays and remove the
out-of-range data from the plot. Importantly, the
``transform`` method *must* return an array that is the
same shape as the input array, since these values need to
remain synchronized with values in the other dimension.
"""
masked
=
ma
.
masked_where
((
a
<
-
self
.
thresh
)
|
(
a
>
self
.
thresh
),
a
)
if
masked
.
mask
.
any
():
return
ma
.
log
(
np
.
abs
(
ma
.
tan
(
masked
)
+
1.0
/
ma
.
cos
(
masked
)))
else
:
return
np
.
log
(
np
.
abs
(
np
.
tan
(
a
)
+
1.0
/
np
.
cos
(
a
)))
def
inverted
(
self
):
"""
Override this method so matplotlib knows how to get the
inverse transform for this transform.
"""
return
MercatorLatitudeScale
.
InvertedMercatorLatitudeTransform
(
self
.
thresh
)
class
InvertedMercatorLatitudeTransform
(
mtransforms
.
Transform
):
input_dims
=
1
output_dims
=
1
is_separable
=
True
def
__init__
(
self
,
thresh
):
mtransforms
.
Transform
.
__init__
(
self
)
self
.
thresh
=
thresh
def
transform_non_affine
(
self
,
a
):
return
np
.
arctan
(
np
.
sinh
(
a
))
def
inverted
(
self
):
return
MercatorLatitudeScale
.
MercatorLatitudeTransform
(
self
.
thresh
)
# Now that the Scale class has been defined, it must be registered so
# that ``matplotlib`` can find it.
mscale
.
register_scale
(
MercatorLatitudeScale
)
if
__name__
==
'__main__'
:
import
matplotlib
.
pyplot
as
plt
t
=
np
.
arange
(
-
180.0
,
180.0
,
0.1
)
s
=
t
/
360.0
*
np
.
pi
plt
.
plot
(
t
,
s
,
'-'
,
lw
=
2
)
plt
.
gca
().
set_yscale
(
'mercator'
)
plt
.
xlabel
(
'Longitude'
)
plt
.
ylabel
(
'Latitude'
)
plt
.
title
(
'Mercator: Projection of the Oppressor'
)
plt
.
grid
(
True
)
plt
.
show
()
Back
|
FazBrowse Home
|
New Git URL