質問編集履歴
3
誤字編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -298,7 +298,7 @@
|
|
298
298
|
|
299
299
|
|
300
300
|
|
301
|
-
|
301
|
+
原因となっていそうなのが
|
302
302
|
|
303
303
|
```Python
|
304
304
|
|
2
コードを中心とした質問内容の編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,10 +2,14 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
|
6
|
+
|
5
7
|
Python3系でレーダーチャートを作成したく
|
6
8
|
|
7
9
|
以下のサイトを参考に作成しておりました.
|
8
10
|
|
11
|
+
https://matplotlib.org/examples/api/radar_chart.html
|
12
|
+
|
9
13
|
https://teratail.com/questions/104066
|
10
14
|
|
11
15
|
|
@@ -14,7 +18,11 @@
|
|
14
18
|
|
15
19
|
|
16
20
|
|
17
|
-
|
21
|
+
本来であれば下図になるものが
|
22
|
+
|
23
|
+
![イメージ説明](a161b27d064c112b7e068e294f32d6bd.png)
|
24
|
+
|
25
|
+
|
18
26
|
|
19
27
|
私の結果は以下のようになってしまいます.
|
20
28
|
|
@@ -24,6 +32,272 @@
|
|
24
32
|
|
25
33
|
|
26
34
|
|
35
|
+
###コード
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
使用したコードは以下になります.
|
40
|
+
|
41
|
+
コピペコードですので元のサイトを一緒に掲載させていただきます.
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
まずhttps://matplotlib.org/examples/api/radar_chart.htmlより
|
46
|
+
|
47
|
+
実際にレーダーチャートを作成する関数をお借りしました.
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
```Python
|
52
|
+
|
53
|
+
def radar_factory(num_vars, frame='circle'):
|
54
|
+
|
55
|
+
"""Create a radar chart with `num_vars` axes.
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
This function creates a RadarAxes projection and registers it.
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
Parameters
|
64
|
+
|
65
|
+
----------
|
66
|
+
|
67
|
+
num_vars : int
|
68
|
+
|
69
|
+
Number of variables for radar chart.
|
70
|
+
|
71
|
+
frame : {'circle' | 'polygon'}
|
72
|
+
|
73
|
+
Shape of frame surrounding axes.
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
"""
|
78
|
+
|
79
|
+
# calculate evenly-spaced axis angles
|
80
|
+
|
81
|
+
theta = np.linspace(0, 2*np.pi, num_vars, endpoint=False)
|
82
|
+
|
83
|
+
# rotate theta such that the first axis is at the top
|
84
|
+
|
85
|
+
theta += np.pi/2
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
def draw_poly_patch(self):
|
90
|
+
|
91
|
+
verts = unit_poly_verts(theta)
|
92
|
+
|
93
|
+
return plt.Polygon(verts, closed=True, edgecolor='k')
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
def draw_circle_patch(self):
|
98
|
+
|
99
|
+
# unit circle centered on (0.5, 0.5)
|
100
|
+
|
101
|
+
return plt.Circle((0.5, 0.5), 0.5)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
patch_dict = {'polygon': draw_poly_patch, 'circle': draw_circle_patch}
|
106
|
+
|
107
|
+
if frame not in patch_dict:
|
108
|
+
|
109
|
+
raise ValueError('unknown value for `frame`: %s' % frame)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
class RadarAxes(PolarAxes):
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
name = 'radar'
|
118
|
+
|
119
|
+
# use 1 line segment to connect specified points
|
120
|
+
|
121
|
+
RESOLUTION = 1
|
122
|
+
|
123
|
+
# define draw_frame method
|
124
|
+
|
125
|
+
draw_patch = patch_dict[frame]
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def fill(self, *args, **kwargs):
|
130
|
+
|
131
|
+
"""Override fill so that line is closed by default"""
|
132
|
+
|
133
|
+
closed = kwargs.pop('closed', True)
|
134
|
+
|
135
|
+
return super(RadarAxes, self).fill(closed=closed, *args, **kwargs)
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
def plot(self, *args, **kwargs):
|
140
|
+
|
141
|
+
"""Override plot so that line is closed by default"""
|
142
|
+
|
143
|
+
lines = super(RadarAxes, self).plot(*args, **kwargs)
|
144
|
+
|
145
|
+
for line in lines:
|
146
|
+
|
147
|
+
self._close_line(line)
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
def _close_line(self, line):
|
152
|
+
|
153
|
+
x, y = line.get_data()
|
154
|
+
|
155
|
+
# FIXME: markers at x[0], y[0] get doubled-up
|
156
|
+
|
157
|
+
if x[0] != x[-1]:
|
158
|
+
|
159
|
+
x = np.concatenate((x, [x[0]]))
|
160
|
+
|
161
|
+
y = np.concatenate((y, [y[0]]))
|
162
|
+
|
163
|
+
line.set_data(x, y)
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
def set_varlabels(self, labels):
|
168
|
+
|
169
|
+
self.set_thetagrids(np.degrees(theta), labels)
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
def _gen_axes_patch(self):
|
174
|
+
|
175
|
+
return self.draw_patch()
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
def _gen_axes_spines(self):
|
180
|
+
|
181
|
+
if frame == 'circle':
|
182
|
+
|
183
|
+
return PolarAxes._gen_axes_spines(self)
|
184
|
+
|
185
|
+
# The following is a hack to get the spines (i.e. the axes frame)
|
186
|
+
|
187
|
+
# to draw correctly for a polygon frame.
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
# spine_type must be 'left', 'right', 'top', 'bottom', or `circle`.
|
192
|
+
|
193
|
+
spine_type = 'circle'
|
194
|
+
|
195
|
+
verts = unit_poly_verts(theta)
|
196
|
+
|
197
|
+
# close off polygon by repeating first vertex
|
198
|
+
|
199
|
+
verts.append(verts[0])
|
200
|
+
|
201
|
+
path = Path(verts)
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
spine = Spine(self, spine_type, path)
|
206
|
+
|
207
|
+
spine.set_transform(self.transAxes)
|
208
|
+
|
209
|
+
return {'polar': spine}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
register_projection(RadarAxes)
|
214
|
+
|
215
|
+
return theta
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
def unit_poly_verts(theta):
|
222
|
+
|
223
|
+
"""Return vertices of polygon for subplot axes.
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
This polygon is circumscribed by a unit circle centered at (0.5, 0.5)
|
228
|
+
|
229
|
+
"""
|
230
|
+
|
231
|
+
x0, y0, r = [0.5] * 3
|
232
|
+
|
233
|
+
verts = [(r*np.cos(t) + x0, r*np.sin(t) + y0) for t in theta]
|
234
|
+
|
235
|
+
return verts
|
236
|
+
|
237
|
+
```
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
その後,magichan様によるコード(掲載元→https://teratail.com/questions/104066)によって整形いたしました.
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
```Python
|
248
|
+
|
249
|
+
N = 9
|
250
|
+
|
251
|
+
theta = radar_factory(N, frame='polygon')
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
label= ['A','B','C','D','E','F','G','H','I']
|
256
|
+
|
257
|
+
data = [3.0, 0.00, 2.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00]
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
ax = plt.subplot(projection='radar')
|
262
|
+
|
263
|
+
# chartの範囲を0-3
|
264
|
+
|
265
|
+
ax.set_ylim(0, 3)
|
266
|
+
|
267
|
+
# Grid線を位置の指定
|
268
|
+
|
269
|
+
ax.set_rgrids([1, 2, 3])
|
270
|
+
|
271
|
+
# 描画処理
|
272
|
+
|
273
|
+
ax.plot(theta, data, 'c.-')
|
274
|
+
|
275
|
+
ax.fill(theta, data, facecolor='cyan', alpha=0.3)
|
276
|
+
|
277
|
+
ax.set_varlabels(label)
|
278
|
+
|
279
|
+
# 標準のグリッド線は円形なので消す(放射方向だけ残す)
|
280
|
+
|
281
|
+
ax.yaxis.grid(False)
|
282
|
+
|
283
|
+
# 替わりグリッド線を描く
|
284
|
+
|
285
|
+
ax.plot(theta, [1]*N, 'k-', marker=None, linewidth=0.5, alpha=0.3)
|
286
|
+
|
287
|
+
ax.plot(theta, [2]*N, 'k-', marker=None, linewidth=0.5, alpha=0.3)
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
plt.show()
|
292
|
+
|
293
|
+
```
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
###やってみたこと
|
298
|
+
|
299
|
+
|
300
|
+
|
27
301
|
悪さをしていそうなのが
|
28
302
|
|
29
303
|
```Python
|
@@ -42,8 +316,6 @@
|
|
42
316
|
|
43
317
|
|
44
318
|
|
45
|
-
何とかして軸ラベルをつけたいと思っております.
|
46
|
-
|
47
319
|
考えられる可能性や解決方法がございましたら
|
48
320
|
|
49
321
|
ご教授いただきたいです.
|
1
初心者マークの付与
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|