Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c4d7f7

Browse files
committed
Simplify radar_chart example.
... by reusing already existing utilities to generate regular polygons.
1 parent dab005d commit 7c4d7f7

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

examples/specialty_plots/radar_chart.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
1414
.. [1] http://en.wikipedia.org/wiki/Radar_chart
1515
"""
16+
1617
import numpy as np
1718

1819
import matplotlib.pyplot as plt
20+
from matplotlib.patches import Circle, RegularPolygon
1921
from matplotlib.path import Path
20-
from matplotlib.spines import Spine
2122
from matplotlib.projections.polar import PolarAxes
2223
from matplotlib.projections import register_projection
24+
from matplotlib.spines import Spine
25+
from matplotlib.transforms import Affine2D
2326

2427

2528
def radar_factory(num_vars, frame='circle'):
@@ -38,14 +41,14 @@ def radar_factory(num_vars, frame='circle'):
3841
# calculate evenly-spaced axis angles
3942
theta = np.linspace(0, 2*np.pi, num_vars, endpoint=False)
4043

44+
# The Axes patch must be centered at (0.5, 0.5) and of radius 0.5 in axes
45+
# coordinates.
46+
4147
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")
4549

4650
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)
4952

5053
patch_dict = {'polygon': draw_poly_patch, 'circle': draw_circle_patch}
5154
if frame not in patch_dict:
@@ -94,31 +97,21 @@ def _gen_axes_spines(self):
9497
# The following is a hack to get the spines (i.e. the axes frame)
9598
# to draw correctly for a polygon frame.
9699

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)
106109
return {'polar': spine}
107110

108111
register_projection(RadarAxes)
109112
return theta
110113

111114

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-
122115
def example_data():
123116
# The following data is from the Denver Aerosol Sources and Health study.
124117
# See doi:10.1016/j.atmosenv.2008.12.017

0 commit comments

Comments
 (0)