1
+ import math
1
2
import numpy as np
2
3
3
4
from matplotlib import _api , rcParams
@@ -52,8 +53,8 @@ def cla(self):
52
53
53
54
self .grid (rcParams ['axes.grid' ])
54
55
55
- Axes .set_xlim (self , - np .pi , np .pi )
56
- Axes .set_ylim (self , - np .pi / 2.0 , np .pi / 2.0 )
56
+ Axes .set_xlim (self , - math .pi , math .pi )
57
+ Axes .set_ylim (self , - math .pi / 2.0 , math .pi / 2.0 )
57
58
58
59
def _set_lim_and_transforms (self ):
59
60
# A (possibly non-linear) projection on the (already scaled) data
@@ -88,7 +89,7 @@ def _set_lim_and_transforms(self):
88
89
Affine2D ().translate (0 , - 4 )
89
90
90
91
# This is the transform for latitude ticks.
91
- yaxis_stretch = Affine2D ().scale (np .pi * 2 , 1 ).translate (- np .pi , 0 )
92
+ yaxis_stretch = Affine2D ().scale (math .pi * 2 , 1 ).translate (- math .pi , 0 )
92
93
yaxis_space = Affine2D ().scale (1 , 1.1 )
93
94
self ._yaxis_transform = \
94
95
yaxis_stretch + \
@@ -108,8 +109,8 @@ def _set_lim_and_transforms(self):
108
109
109
110
def _get_affine_transform (self ):
110
111
transform = self ._get_core_transform (1 )
111
- xscale , _ = transform .transform ((np .pi , 0 ))
112
- _ , yscale = transform .transform ((0 , np .pi / 2 ))
112
+ xscale , _ = transform .transform ((math .pi , 0 ))
113
+ _ , yscale = transform .transform ((0 , math .pi / 2 ))
113
114
return Affine2D () \
114
115
.scale (0.5 / xscale , 0.5 / yscale ) \
115
116
.translate (0.5 , 0.5 )
@@ -287,7 +288,7 @@ def inverted(self):
287
288
return AitoffAxes .AitoffTransform (self ._resolution )
288
289
289
290
def __init__ (self , * args , ** kwargs ):
290
- self ._longitude_cap = np .pi / 2.0
291
+ self ._longitude_cap = math .pi / 2
291
292
super ().__init__ (* args , ** kwargs )
292
293
self .set_aspect (0.5 , adjustable = 'box' , anchor = 'C' )
293
294
self .cla ()
@@ -305,11 +306,11 @@ class HammerTransform(_GeoTransform):
305
306
def transform_non_affine (self , ll ):
306
307
# docstring inherited
307
308
longitude , latitude = ll .T
308
- half_long = longitude / 2.0
309
+ half_long = longitude / 2
309
310
cos_latitude = np .cos (latitude )
310
- sqrt2 = np . sqrt ( 2.0 )
311
+ sqrt2 = 2 ** ( 1 / 2 )
311
312
alpha = np .sqrt (1.0 + cos_latitude * np .cos (half_long ))
312
- x = (2.0 * sqrt2 ) * (cos_latitude * np .sin (half_long )) / alpha
313
+ x = (2 * sqrt2 ) * (cos_latitude * np .sin (half_long )) / alpha
313
314
y = (sqrt2 * np .sin (latitude )) / alpha
314
315
return np .column_stack ([x , y ])
315
316
@@ -324,15 +325,15 @@ def transform_non_affine(self, xy):
324
325
x , y = xy .T
325
326
z = np .sqrt (1 - (x / 4 ) ** 2 - (y / 2 ) ** 2 )
326
327
longitude = 2 * np .arctan ((z * x ) / (2 * (2 * z ** 2 - 1 )))
327
- latitude = np .arcsin (y * z )
328
+ latitude = np .arcsin (y * z )
328
329
return np .column_stack ([longitude , latitude ])
329
330
330
331
def inverted (self ):
331
332
# docstring inherited
332
333
return HammerAxes .HammerTransform (self ._resolution )
333
334
334
335
def __init__ (self , * args , ** kwargs ):
335
- self ._longitude_cap = np .pi / 2.0
336
+ self ._longitude_cap = math .pi / 2
336
337
super ().__init__ (* args , ** kwargs )
337
338
self .set_aspect (0.5 , adjustable = 'box' , anchor = 'C' )
338
339
self .cla ()
@@ -351,18 +352,18 @@ def transform_non_affine(self, ll):
351
352
# docstring inherited
352
353
def d (theta ):
353
354
delta = (- (theta + np .sin (theta ) - pi_sin_l )
354
- / (1 + np .cos (theta )))
355
+ / (1.0 + np .cos (theta )))
355
356
return delta , np .abs (delta ) > 0.001
356
357
357
358
longitude , latitude = ll .T
358
-
359
- clat = np . pi / 2 - np .abs (latitude )
359
+ pi_half = math . pi / 2
360
+ clat = pi_half - np .abs (latitude )
360
361
ihigh = clat < 0.087 # within 5 degrees of the poles
361
362
ilow = ~ ihigh
362
363
aux = np .empty (latitude .shape , dtype = float )
363
364
364
365
if ilow .any (): # Newton-Raphson iteration
365
- pi_sin_l = np .pi * np .sin (latitude [ilow ])
366
+ pi_sin_l = math .pi * np .sin (latitude [ilow ])
366
367
theta = 2.0 * latitude [ilow ]
367
368
delta , large_delta = d (theta )
368
369
while np .any (large_delta ):
@@ -372,12 +373,13 @@ def d(theta):
372
373
373
374
if ihigh .any (): # Taylor series-based approx. solution
374
375
e = clat [ihigh ]
375
- d = 0.5 * ( 3 * np .pi * e ** 2 ) ** ( 1.0 / 3 )
376
- aux [ihigh ] = (np . pi / 2 - d ) * np .sign (latitude [ihigh ])
376
+ d = np . cbrt ( 3.0 * math .pi * e ** 2 ) / 2
377
+ aux [ihigh ] = (pi_half - d ) * np .sign (latitude [ihigh ])
377
378
378
379
xy = np .empty (ll .shape , dtype = float )
379
- xy [:, 0 ] = (2.0 * np .sqrt (2.0 ) / np .pi ) * longitude * np .cos (aux )
380
- xy [:, 1 ] = np .sqrt (2.0 ) * np .sin (aux )
380
+ sqrt2 = 2 ** (1 / 2 )
381
+ xy [:, 0 ] = (sqrt2 / pi_half ) * longitude * np .cos (aux )
382
+ xy [:, 1 ] = sqrt2 * np .sin (aux )
381
383
382
384
return xy
383
385
@@ -392,17 +394,18 @@ def transform_non_affine(self, xy):
392
394
x , y = xy .T
393
395
# from Equations (7, 8) of
394
396
# https://mathworld.wolfram.com/MollweideProjection.html
395
- theta = np .arcsin (y / np .sqrt (2 ))
396
- longitude = (np .pi / (2 * np .sqrt (2 ))) * x / np .cos (theta )
397
- latitude = np .arcsin ((2 * theta + np .sin (2 * theta )) / np .pi )
397
+ sqrt2 = 2 ** (1 / 2 )
398
+ theta = np .arcsin (y / sqrt2 )
399
+ longitude = (math .pi / (2.0 * sqrt2 )) * x / np .cos (theta )
400
+ latitude = np .arcsin ((2.0 * theta + np .sin (2.0 * theta )) / math .pi )
398
401
return np .column_stack ([longitude , latitude ])
399
402
400
403
def inverted (self ):
401
404
# docstring inherited
402
405
return MollweideAxes .MollweideTransform (self ._resolution )
403
406
404
407
def __init__ (self , * args , ** kwargs ):
405
- self ._longitude_cap = np .pi / 2.0
408
+ self ._longitude_cap = math .pi / 2
406
409
super ().__init__ (* args , ** kwargs )
407
410
self .set_aspect (0.5 , adjustable = 'box' , anchor = 'C' )
408
411
self .cla ()
@@ -434,15 +437,17 @@ def transform_non_affine(self, ll):
434
437
clat = self ._center_latitude
435
438
cos_lat = np .cos (latitude )
436
439
sin_lat = np .sin (latitude )
440
+ cos_clat = np .cos (clat )
441
+ sin_clat = np .sin (clat )
437
442
diff_long = longitude - clong
438
443
cos_diff_long = np .cos (diff_long )
439
444
440
445
inner_k = np .maximum ( # Prevent divide-by-zero problems
441
- 1 + np . sin ( clat ) * sin_lat + np . cos ( clat ) * cos_lat * cos_diff_long ,
446
+ 1.0 + sin_clat * sin_lat + cos_clat * cos_lat * cos_diff_long ,
442
447
1e-15 )
443
- k = np .sqrt (2 / inner_k )
448
+ k = np .sqrt (2.0 / inner_k )
444
449
x = k * cos_lat * np .sin (diff_long )
445
- y = k * (np . cos ( clat ) * sin_lat - np . sin ( clat ) * cos_lat * cos_diff_long )
450
+ y = k * (cos_clat * sin_lat - sin_clat * cos_lat * cos_diff_long )
446
451
447
452
return np .column_stack ([x , y ])
448
453
@@ -466,7 +471,7 @@ def transform_non_affine(self, xy):
466
471
clong = self ._center_longitude
467
472
clat = self ._center_latitude
468
473
p = np .maximum (np .hypot (x , y ), 1e-9 )
469
- c = 2 * np .arcsin (0.5 * p )
474
+ c = 2.0 * np .arcsin (0.5 * p )
470
475
sin_c = np .sin (c )
471
476
cos_c = np .cos (c )
472
477
@@ -485,7 +490,7 @@ def inverted(self):
485
490
self ._resolution )
486
491
487
492
def __init__ (self , * args , center_longitude = 0 , center_latitude = 0 , ** kwargs ):
488
- self ._longitude_cap = np .pi / 2
493
+ self ._longitude_cap = math .pi / 2
489
494
self ._center_longitude = center_longitude
490
495
self ._center_latitude = center_latitude
491
496
super ().__init__ (* args , ** kwargs )
0 commit comments