1
1
"""
2
2
=====================
3
- Demo Curvelinear Grid
3
+ Curvilinear grid demo
4
4
=====================
5
5
6
6
Custom grid and ticklines.
7
7
8
- This example demonstrates how to use GridHelperCurveLinear to define
9
- custom grids and ticklines by applying a transformation on the grid.
10
- This can be used, as showcase on the second plot, to create polar
11
- projections in a rectangular box.
8
+ This example demonstrates how to use
9
+ `~.grid_helper_curvelinear.GridHelperCurveLinear` to define custom grids and
10
+ ticklines by applying a transformation on the grid. This can be used, as
11
+ shown on the second plot, to create polar projections in a rectangular box.
12
12
"""
13
13
14
14
import numpy as np
15
15
16
16
import matplotlib .pyplot as plt
17
- import matplotlib .cbook as cbook
17
+ from matplotlib .projections import PolarAxes
18
+ from matplotlib .transforms import Affine2D
18
19
19
- from mpl_toolkits .axisartist import Subplot
20
- from mpl_toolkits .axisartist import SubplotHost , \
21
- ParasiteAxesAuxTrans
22
- from mpl_toolkits .axisartist .grid_helper_curvelinear import \
23
- GridHelperCurveLinear
20
+ from mpl_toolkits .axisartist import (
21
+ angle_helper , Subplot , SubplotHost , ParasiteAxesAuxTrans )
22
+ from mpl_toolkits .axisartist .grid_helper_curvelinear import (
23
+ GridHelperCurveLinear )
24
24
25
25
26
26
def curvelinear_test1 (fig ):
27
27
"""
28
- grid for custom transform.
28
+ Grid for custom transform.
29
29
"""
30
30
31
31
def tr (x , y ):
@@ -46,89 +46,68 @@ def inv_tr(x, y):
46
46
47
47
fig .add_subplot (ax1 )
48
48
49
- xx , yy = tr ([3 , 6 ], [5.0 , 10. ])
49
+ xx , yy = tr ([3 , 6 ], [5 , 10 ])
50
50
ax1 .plot (xx , yy , linewidth = 2.0 )
51
51
52
- ax1 .set_aspect (1. )
53
- ax1 .set_xlim (0 , 10. )
54
- ax1 .set_ylim (0 , 10. )
52
+ ax1 .set_aspect (1 )
53
+ ax1 .set_xlim (0 , 10 )
54
+ ax1 .set_ylim (0 , 10 )
55
55
56
- ax1 .axis ["t" ] = ax1 .new_floating_axis (0 , 3. )
57
- ax1 .axis ["t2" ] = ax1 .new_floating_axis (1 , 7. )
56
+ ax1 .axis ["t" ] = ax1 .new_floating_axis (0 , 3 )
57
+ ax1 .axis ["t2" ] = ax1 .new_floating_axis (1 , 7 )
58
58
ax1 .grid (True , zorder = 0 )
59
59
60
60
61
- import mpl_toolkits .axisartist .angle_helper as angle_helper
62
-
63
- from matplotlib .projections import PolarAxes
64
- from matplotlib .transforms import Affine2D
65
-
66
-
67
61
def curvelinear_test2 (fig ):
68
62
"""
69
- polar projection, but in a rectangular box.
63
+ Polar projection, but in a rectangular box.
70
64
"""
71
65
72
66
# PolarAxes.PolarTransform takes radian. However, we want our coordinate
73
67
# system in degree
74
- tr = Affine2D ().scale (np .pi / 180. , 1. ) + PolarAxes .PolarTransform ()
75
-
76
- # polar projection, which involves cycle, and also has limits in
68
+ tr = Affine2D ().scale (np .pi / 180 , 1 ) + PolarAxes .PolarTransform ()
69
+ # Polar projection, which involves cycle, and also has limits in
77
70
# its coordinates, needs a special method to find the extremes
78
71
# (min, max of the coordinate within the view).
79
-
80
- # 20, 20 : number of sampling points along x, y direction
81
- extreme_finder = angle_helper .ExtremeFinderCycle (20 , 20 ,
82
- lon_cycle = 360 ,
83
- lat_cycle = None ,
84
- lon_minmax = None ,
85
- lat_minmax = (0 , np .inf ),
86
- )
87
-
72
+ extreme_finder = angle_helper .ExtremeFinderCycle (
73
+ nx = 20 , ny = 20 , # Number of sampling points in each direction.
74
+ lon_cycle = 360 , lat_cycle = None ,
75
+ lon_minmax = None , lat_minmax = (0 , np .inf ),
76
+ )
77
+ # Find grid values appropriate for the coordinate (degree, minute, second).
88
78
grid_locator1 = angle_helper .LocatorDMS (12 )
89
- # Find a grid values appropriate for the coordinate (degree,
90
- # minute, second).
91
-
79
+ # Use an appropriate formatter. Note that the acceptable Locator and
80
+ # Formatter classes are a bit different than that of Matplotlib, which
81
+ # cannot directly be used here (this may be possible in the future).
92
82
tick_formatter1 = angle_helper .FormatterDMS ()
93
- # And also uses an appropriate formatter. Note that,the
94
- # acceptable Locator and Formatter class is a bit different than
95
- # that of mpl's, and you cannot directly use mpl's Locator and
96
- # Formatter here (but may be possible in the future).
97
-
98
- grid_helper = GridHelperCurveLinear (tr ,
99
- extreme_finder = extreme_finder ,
100
- grid_locator1 = grid_locator1 ,
101
- tick_formatter1 = tick_formatter1
102
- )
103
83
84
+ grid_helper = GridHelperCurveLinear (
85
+ tr , extreme_finder = extreme_finder ,
86
+ grid_locator1 = grid_locator1 , tick_formatter1 = tick_formatter1 )
104
87
ax1 = SubplotHost (fig , 1 , 2 , 2 , grid_helper = grid_helper )
105
88
106
89
# make ticklabels of right and top axis visible.
107
90
ax1 .axis ["right" ].major_ticklabels .set_visible (True )
108
91
ax1 .axis ["top" ].major_ticklabels .set_visible (True )
109
-
110
92
# let right axis shows ticklabels for 1st coordinate (angle)
111
93
ax1 .axis ["right" ].get_helper ().nth_coord_ticks = 0
112
94
# let bottom axis shows ticklabels for 2nd coordinate (radius)
113
95
ax1 .axis ["bottom" ].get_helper ().nth_coord_ticks = 1
114
96
115
97
fig .add_subplot (ax1 )
116
98
99
+ ax1 .set_aspect (1 )
100
+ ax1 .set_xlim (- 5 , 12 )
101
+ ax1 .set_ylim (- 5 , 10 )
102
+
103
+ ax1 .grid (True , zorder = 0 )
104
+
117
105
# A parasite axes with given transform
118
106
ax2 = ParasiteAxesAuxTrans (ax1 , tr , "equal" )
119
107
# note that ax2.transData == tr + ax1.transData
120
108
# Anything you draw in ax2 will match the ticks and grids of ax1.
121
109
ax1 .parasites .append (ax2 )
122
- intp = cbook .simple_linear_interpolation
123
- ax2 .plot (intp (np .array ([0 , 30 ]), 50 ),
124
- intp (np .array ([10. , 10. ]), 50 ),
125
- linewidth = 2.0 )
126
-
127
- ax1 .set_aspect (1. )
128
- ax1 .set_xlim (- 5 , 12 )
129
- ax1 .set_ylim (- 5 , 10 )
130
-
131
- ax1 .grid (True , zorder = 0 )
110
+ ax2 .plot (np .linspace (0 , 30 , 51 ), np .linspace (10 , 10 , 51 ), linewidth = 2 )
132
111
133
112
134
113
if __name__ == "__main__" :
0 commit comments