alvi123 commited on
Commit
dac4f0d
1 Parent(s): c59d0a4
Files changed (1) hide show
  1. radar_chart.py +198 -0
radar_chart.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ======================================
3
+ Radar chart (aka spider or star chart)
4
+ ======================================
5
+ This example creates a radar chart, also known as a spider or star chart [1]_.
6
+ Although this example allows a frame of either 'circle' or 'polygon', polygon
7
+ frames don't have proper gridlines (the lines are circles instead of polygons).
8
+ It's possible to get a polygon grid by setting GRIDLINE_INTERPOLATION_STEPS in
9
+ matplotlib.axis to the desired number of vertices, but the orientation of the
10
+ polygon is not aligned with the radial axes.
11
+ .. [1] https://en.wikipedia.org/wiki/Radar_chart
12
+ """
13
+
14
+ import numpy as np
15
+
16
+ import matplotlib.pyplot as plt
17
+ from matplotlib.patches import Circle, RegularPolygon
18
+ from matplotlib.path import Path
19
+ from matplotlib.projections.polar import PolarAxes
20
+ from matplotlib.projections import register_projection
21
+ from matplotlib.spines import Spine
22
+ from matplotlib.transforms import Affine2D
23
+
24
+
25
+ def radar_factory(num_vars, frame='circle'):
26
+ """
27
+ Create a radar chart with `num_vars` axes.
28
+ This function creates a RadarAxes projection and registers it.
29
+ Parameters
30
+ ----------
31
+ num_vars : int
32
+ Number of variables for radar chart.
33
+ frame : {'circle', 'polygon'}
34
+ Shape of frame surrounding axes.
35
+ """
36
+ # calculate evenly-spaced axis angles
37
+ theta = np.linspace(0, 2*np.pi, num_vars, endpoint=False)
38
+
39
+ class RadarTransform(PolarAxes.PolarTransform):
40
+
41
+ def transform_path_non_affine(self, path):
42
+ # Paths with non-unit interpolation steps correspond to gridlines,
43
+ # in which case we force interpolation (to defeat PolarTransform's
44
+ # autoconversion to circular arcs).
45
+ if path._interpolation_steps > 1:
46
+ path = path.interpolated(num_vars)
47
+ return Path(self.transform(path.vertices), path.codes)
48
+
49
+ class RadarAxes(PolarAxes):
50
+
51
+ name = 'radar'
52
+ PolarTransform = RadarTransform
53
+
54
+ def __init__(self, *args, **kwargs):
55
+ super().__init__(*args, **kwargs)
56
+ # rotate plot such that the first axis is at the top
57
+ self.set_theta_zero_location('N')
58
+
59
+ def fill(self, *args, closed=True, **kwargs):
60
+ """Override fill so that line is closed by default"""
61
+ return super().fill(closed=closed, *args, **kwargs)
62
+
63
+ def plot(self, *args, **kwargs):
64
+ """Override plot so that line is closed by default"""
65
+ lines = super().plot(*args, **kwargs)
66
+ for line in lines:
67
+ self._close_line(line)
68
+
69
+ def _close_line(self, line):
70
+ x, y = line.get_data()
71
+ # FIXME: markers at x[0], y[0] get doubled-up
72
+ if x[0] != x[-1]:
73
+ x = np.append(x, x[0])
74
+ y = np.append(y, y[0])
75
+ line.set_data(x, y)
76
+
77
+ def set_varlabels(self, labels):
78
+ self.set_thetagrids(np.degrees(theta), labels)
79
+
80
+ def _gen_axes_patch(self):
81
+ # The Axes patch must be centered at (0.5, 0.5) and of radius 0.5
82
+ # in axes coordinates.
83
+ if frame == 'circle':
84
+ return Circle((0.5, 0.5), 0.5)
85
+ elif frame == 'polygon':
86
+ return RegularPolygon((0.5, 0.5), num_vars,
87
+ radius=.5, edgecolor="k")
88
+ else:
89
+ raise ValueError("Unknown value for 'frame': %s" % frame)
90
+
91
+ def _gen_axes_spines(self):
92
+ if frame == 'circle':
93
+ return super()._gen_axes_spines()
94
+ elif frame == 'polygon':
95
+ # spine_type must be 'left'/'right'/'top'/'bottom'/'circle'.
96
+ spine = Spine(axes=self,
97
+ spine_type='circle',
98
+ path=Path.unit_regular_polygon(num_vars))
99
+ # unit_regular_polygon gives a polygon of radius 1 centered at
100
+ # (0, 0) but we want a polygon of radius 0.5 centered at (0.5,
101
+ # 0.5) in axes coordinates.
102
+ spine.set_transform(Affine2D().scale(.5).translate(.5, .5)
103
+ + self.transAxes)
104
+ return {'polar': spine}
105
+ else:
106
+ raise ValueError("Unknown value for 'frame': %s" % frame)
107
+
108
+ register_projection(RadarAxes)
109
+ return theta
110
+
111
+
112
+ def example_data():
113
+ # The following data is from the Denver Aerosol Sources and Health study.
114
+ # See doi:10.1016/j.atmosenv.2008.12.017
115
+ #
116
+ # The data are pollution source profile estimates for five modeled
117
+ # pollution sources (e.g., cars, wood-burning, etc) that emit 7-9 chemical
118
+ # species. The radar charts are experimented with here to see if we can
119
+ # nicely visualize how the modeled source profiles change across four
120
+ # scenarios:
121
+ # 1) No gas-phase species present, just seven particulate counts on
122
+ # Sulfate
123
+ # Nitrate
124
+ # Elemental Carbon (EC)
125
+ # Organic Carbon fraction 1 (OC)
126
+ # Organic Carbon fraction 2 (OC2)
127
+ # Organic Carbon fraction 3 (OC3)
128
+ # Pyrolyzed Organic Carbon (OP)
129
+ # 2)Inclusion of gas-phase specie carbon monoxide (CO)
130
+ # 3)Inclusion of gas-phase specie ozone (O3).
131
+ # 4)Inclusion of both gas-phase species is present...
132
+ data = [
133
+ ['Sulfate', 'Nitrate', 'EC', 'OC1', 'OC2', 'OC3', 'OP', 'CO', 'O3'],
134
+ ('Basecase', [
135
+ [0.88, 0.01, 0.03, 0.03, 0.00, 0.06, 0.01, 0.00, 0.00],
136
+ [0.07, 0.95, 0.04, 0.05, 0.00, 0.02, 0.01, 0.00, 0.00],
137
+ [0.01, 0.02, 0.85, 0.19, 0.05, 0.10, 0.00, 0.00, 0.00],
138
+ [0.02, 0.01, 0.07, 0.01, 0.21, 0.12, 0.98, 0.00, 0.00],
139
+ [0.01, 0.01, 0.02, 0.71, 0.74, 0.70, 0.00, 0.00, 0.00]]),
140
+ ('With CO', [
141
+ [0.88, 0.02, 0.02, 0.02, 0.00, 0.05, 0.00, 0.05, 0.00],
142
+ [0.08, 0.94, 0.04, 0.02, 0.00, 0.01, 0.12, 0.04, 0.00],
143
+ [0.01, 0.01, 0.79, 0.10, 0.00, 0.05, 0.00, 0.31, 0.00],
144
+ [0.00, 0.02, 0.03, 0.38, 0.31, 0.31, 0.00, 0.59, 0.00],
145
+ [0.02, 0.02, 0.11, 0.47, 0.69, 0.58, 0.88, 0.00, 0.00]]),
146
+ ('With O3', [
147
+ [0.89, 0.01, 0.07, 0.00, 0.00, 0.05, 0.00, 0.00, 0.03],
148
+ [0.07, 0.95, 0.05, 0.04, 0.00, 0.02, 0.12, 0.00, 0.00],
149
+ [0.01, 0.02, 0.86, 0.27, 0.16, 0.19, 0.00, 0.00, 0.00],
150
+ [0.01, 0.03, 0.00, 0.32, 0.29, 0.27, 0.00, 0.00, 0.95],
151
+ [0.02, 0.00, 0.03, 0.37, 0.56, 0.47, 0.87, 0.00, 0.00]]),
152
+ ('CO & O3', [
153
+ [0.87, 0.01, 0.08, 0.00, 0.00, 0.04, 0.00, 0.00, 0.01],
154
+ [0.09, 0.95, 0.02, 0.03, 0.00, 0.01, 0.13, 0.06, 0.00],
155
+ [0.01, 0.02, 0.71, 0.24, 0.13, 0.16, 0.00, 0.50, 0.00],
156
+ [0.01, 0.03, 0.00, 0.28, 0.24, 0.23, 0.00, 0.44, 0.88],
157
+ [0.02, 0.00, 0.18, 0.45, 0.64, 0.55, 0.86, 0.00, 0.16]])
158
+ ]
159
+ return data
160
+
161
+
162
+ if __name__ == '__main__':
163
+ N = 8
164
+ theta = radar_factory(N, frame='polygon')
165
+
166
+ # data = example_data()
167
+ # spoke_labels = data.pop(0)
168
+ spoke_labels = np.array(['kata_benda',
169
+ 'kata_kerja',
170
+ 'kata_keterangan',
171
+ 'kata_sifat])
172
+ fig, axs = plt.subplots(figsize=(8, 8), nrows=1, ncols=1,
173
+ subplot_kw=dict(projection='radar'))
174
+ # fig.subplots_adjust(wspace=0.25, hspace=0.20, top=0.85, bottom=0.05)
175
+ vec = np.array([0.1, 0.05, 0.2, 0.05, 0.3, 0, 0.15, 0.15])
176
+ axs.plot(vec)
177
+ axs.set_varlabels(spoke_labels)
178
+ # colors = ['b', 'r', 'g', 'm', 'y']
179
+ # # Plot the four cases from the example data on separate axes
180
+ # for ax, (title, case_data) in zip(axs.flat, data):
181
+ # ax.set_rgrids([0.2, 0.4, 0.6, 0.8])
182
+ # ax.set_title(title, weight='bold', size='medium', position=(0.5, 1.1),
183
+ # horizontalalignment='center', verticalalignment='center')
184
+ # for d, color in zip(case_data, colors):
185
+ # ax.plot(theta, d, color=color)
186
+ # ax.fill(theta, d, facecolor=color, alpha=0.25, label='_nolegend_')
187
+ # ax.set_varlabels(spoke_labels)
188
+
189
+ # # add legend relative to top-left plot
190
+ # labels = ('Factor 1', 'Factor 2', 'Factor 3', 'Factor 4', 'Factor 5')
191
+ # legend = axs[0, 0].legend(labels, loc=(0.9, .95),
192
+ # labelspacing=0.1, fontsize='small')
193
+
194
+ # fig.text(0.5, 0.965, '5-Factor Solution Profiles Across Four Scenarios',
195
+ # horizontalalignment='center', color='black', weight='bold',
196
+ # size='large')
197
+
198
+ plt.show()