13
13
14
14
.. [1] http://en.wikipedia.org/wiki/Radar_chart
15
15
"""
16
+
16
17
import numpy as np
17
18
18
19
import matplotlib .pyplot as plt
20
+ from matplotlib .patches import Circle , RegularPolygon
19
21
from matplotlib .path import Path
20
- from matplotlib .spines import Spine
21
22
from matplotlib .projections .polar import PolarAxes
22
23
from matplotlib .projections import register_projection
24
+ from matplotlib .spines import Spine
25
+ from matplotlib .transforms import Affine2D
23
26
24
27
25
28
def radar_factory (num_vars , frame = 'circle' ):
@@ -38,14 +41,14 @@ def radar_factory(num_vars, frame='circle'):
38
41
# calculate evenly-spaced axis angles
39
42
theta = np .linspace (0 , 2 * np .pi , num_vars , endpoint = False )
40
43
44
+ # The Axes patch must be centered at (0.5, 0.5) and of radius 0.5 in axes
45
+ # coordinates.
46
+
41
47
def draw_poly_patch (self ):
42
- # rotate theta such that the first axis is at the top
43
- verts = unit_poly_verts (theta + np .pi / 2 )
44
- return plt .Polygon (verts , closed = True , edgecolor = 'k' )
48
+ return RegularPolygon ((0.5 , 0.5 ), num_vars , radius = .5 , edgecolor = "k" )
45
49
46
50
def draw_circle_patch (self ):
47
- # unit circle centered on (0.5, 0.5)
48
- return plt .Circle ((0.5 , 0.5 ), 0.5 )
51
+ return Circle ((0.5 , 0.5 ), 0.5 )
49
52
50
53
patch_dict = {'polygon' : draw_poly_patch , 'circle' : draw_circle_patch }
51
54
if frame not in patch_dict :
@@ -94,31 +97,21 @@ def _gen_axes_spines(self):
94
97
# The following is a hack to get the spines (i.e. the axes frame)
95
98
# to draw correctly for a polygon frame.
96
99
97
- # spine_type must be 'left', 'right', 'top', 'bottom', or ` circle` .
98
- spine_type = 'circle'
99
- verts = unit_poly_verts ( theta + np . pi / 2 )
100
- # close off polygon by repeating first vertex
101
- verts . append ( verts [ 0 ])
102
- path = Path ( verts )
103
-
104
- spine = Spine ( self , spine_type , path )
105
- spine . set_transform ( self .transAxes )
100
+ # spine_type must be 'left', 'right', 'top', 'bottom', or ' circle' .
101
+ spine = Spine ( axes = self ,
102
+ spine_type = 'circle' ,
103
+ path = Path . unit_regular_polygon ( num_vars ))
104
+ # unit_regular_polygon gives a polygon of radius 1 centered at
105
+ # (0, 0) but we want a polygon of radius 0.5 centerer at (0.5, 0.5 )
106
+ # in axes coordinates.
107
+ spine . set_transform ( Affine2D (). scale ( .5 ). translate ( .5 , .5 )
108
+ + self .transAxes )
106
109
return {'polar' : spine }
107
110
108
111
register_projection (RadarAxes )
109
112
return theta
110
113
111
114
112
- def unit_poly_verts (theta ):
113
- """Return vertices of polygon for subplot axes.
114
-
115
- This polygon is circumscribed by a unit circle centered at (0.5, 0.5)
116
- """
117
- x0 , y0 , r = [0.5 ] * 3
118
- verts = [(r * np .cos (t ) + x0 , r * np .sin (t ) + y0 ) for t in theta ]
119
- return verts
120
-
121
-
122
115
def example_data ():
123
116
# The following data is from the Denver Aerosol Sources and Health study.
124
117
# See doi:10.1016/j.atmosenv.2008.12.017
0 commit comments