質問編集履歴
3
誤字編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -148,7 +148,7 @@
|
|
148
148
|
|
149
149
|
###やってみたこと
|
150
150
|
|
151
|
-
|
151
|
+
原因となっていそうなのが
|
152
152
|
```Python
|
153
153
|
ax.set_varlabels(label)
|
154
154
|
```
|
2
コードを中心とした質問内容の編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,16 +1,153 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
+
|
3
4
|
Python3系でレーダーチャートを作成したく
|
4
5
|
以下のサイトを参考に作成しておりました.
|
6
|
+
https://matplotlib.org/examples/api/radar_chart.html
|
5
7
|
https://teratail.com/questions/104066
|
6
8
|
|
7
9
|
しかしラベルをつけようとレイアウトが崩れてしまう問題が発生いたしました.
|
8
10
|
|
9
|
-
|
11
|
+
本来であれば下図になるものが
|
12
|
+

|
13
|
+
|
10
14
|
私の結果は以下のようになってしまいます.
|
11
15
|
|
12
16
|

|
13
17
|
|
18
|
+
###コード
|
19
|
+
|
20
|
+
使用したコードは以下になります.
|
21
|
+
コピペコードですので元のサイトを一緒に掲載させていただきます.
|
22
|
+
|
23
|
+
まずhttps://matplotlib.org/examples/api/radar_chart.htmlより
|
24
|
+
実際にレーダーチャートを作成する関数をお借りしました.
|
25
|
+
|
26
|
+
```Python
|
27
|
+
def radar_factory(num_vars, frame='circle'):
|
28
|
+
"""Create a radar chart with `num_vars` axes.
|
29
|
+
|
30
|
+
This function creates a RadarAxes projection and registers it.
|
31
|
+
|
32
|
+
Parameters
|
33
|
+
----------
|
34
|
+
num_vars : int
|
35
|
+
Number of variables for radar chart.
|
36
|
+
frame : {'circle' | 'polygon'}
|
37
|
+
Shape of frame surrounding axes.
|
38
|
+
|
39
|
+
"""
|
40
|
+
# calculate evenly-spaced axis angles
|
41
|
+
theta = np.linspace(0, 2*np.pi, num_vars, endpoint=False)
|
42
|
+
# rotate theta such that the first axis is at the top
|
43
|
+
theta += np.pi/2
|
44
|
+
|
45
|
+
def draw_poly_patch(self):
|
46
|
+
verts = unit_poly_verts(theta)
|
47
|
+
return plt.Polygon(verts, closed=True, edgecolor='k')
|
48
|
+
|
49
|
+
def draw_circle_patch(self):
|
50
|
+
# unit circle centered on (0.5, 0.5)
|
51
|
+
return plt.Circle((0.5, 0.5), 0.5)
|
52
|
+
|
53
|
+
patch_dict = {'polygon': draw_poly_patch, 'circle': draw_circle_patch}
|
54
|
+
if frame not in patch_dict:
|
55
|
+
raise ValueError('unknown value for `frame`: %s' % frame)
|
56
|
+
|
57
|
+
class RadarAxes(PolarAxes):
|
58
|
+
|
59
|
+
name = 'radar'
|
60
|
+
# use 1 line segment to connect specified points
|
61
|
+
RESOLUTION = 1
|
62
|
+
# define draw_frame method
|
63
|
+
draw_patch = patch_dict[frame]
|
64
|
+
|
65
|
+
def fill(self, *args, **kwargs):
|
66
|
+
"""Override fill so that line is closed by default"""
|
67
|
+
closed = kwargs.pop('closed', True)
|
68
|
+
return super(RadarAxes, self).fill(closed=closed, *args, **kwargs)
|
69
|
+
|
70
|
+
def plot(self, *args, **kwargs):
|
71
|
+
"""Override plot so that line is closed by default"""
|
72
|
+
lines = super(RadarAxes, self).plot(*args, **kwargs)
|
73
|
+
for line in lines:
|
74
|
+
self._close_line(line)
|
75
|
+
|
76
|
+
def _close_line(self, line):
|
77
|
+
x, y = line.get_data()
|
78
|
+
# FIXME: markers at x[0], y[0] get doubled-up
|
79
|
+
if x[0] != x[-1]:
|
80
|
+
x = np.concatenate((x, [x[0]]))
|
81
|
+
y = np.concatenate((y, [y[0]]))
|
82
|
+
line.set_data(x, y)
|
83
|
+
|
84
|
+
def set_varlabels(self, labels):
|
85
|
+
self.set_thetagrids(np.degrees(theta), labels)
|
86
|
+
|
87
|
+
def _gen_axes_patch(self):
|
88
|
+
return self.draw_patch()
|
89
|
+
|
90
|
+
def _gen_axes_spines(self):
|
91
|
+
if frame == 'circle':
|
92
|
+
return PolarAxes._gen_axes_spines(self)
|
93
|
+
# The following is a hack to get the spines (i.e. the axes frame)
|
94
|
+
# to draw correctly for a polygon frame.
|
95
|
+
|
96
|
+
# spine_type must be 'left', 'right', 'top', 'bottom', or `circle`.
|
97
|
+
spine_type = 'circle'
|
98
|
+
verts = unit_poly_verts(theta)
|
99
|
+
# close off polygon by repeating first vertex
|
100
|
+
verts.append(verts[0])
|
101
|
+
path = Path(verts)
|
102
|
+
|
103
|
+
spine = Spine(self, spine_type, path)
|
104
|
+
spine.set_transform(self.transAxes)
|
105
|
+
return {'polar': spine}
|
106
|
+
|
107
|
+
register_projection(RadarAxes)
|
108
|
+
return theta
|
109
|
+
|
110
|
+
|
111
|
+
def unit_poly_verts(theta):
|
112
|
+
"""Return vertices of polygon for subplot axes.
|
113
|
+
|
114
|
+
This polygon is circumscribed by a unit circle centered at (0.5, 0.5)
|
115
|
+
"""
|
116
|
+
x0, y0, r = [0.5] * 3
|
117
|
+
verts = [(r*np.cos(t) + x0, r*np.sin(t) + y0) for t in theta]
|
118
|
+
return verts
|
119
|
+
```
|
120
|
+
|
121
|
+
その後,magichan様によるコード(掲載元→https://teratail.com/questions/104066)によって整形いたしました.
|
122
|
+
|
123
|
+
|
124
|
+
```Python
|
125
|
+
N = 9
|
126
|
+
theta = radar_factory(N, frame='polygon')
|
127
|
+
|
128
|
+
label= ['A','B','C','D','E','F','G','H','I']
|
129
|
+
data = [3.0, 0.00, 2.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00]
|
130
|
+
|
131
|
+
ax = plt.subplot(projection='radar')
|
132
|
+
# chartの範囲を0-3
|
133
|
+
ax.set_ylim(0, 3)
|
134
|
+
# Grid線を位置の指定
|
135
|
+
ax.set_rgrids([1, 2, 3])
|
136
|
+
# 描画処理
|
137
|
+
ax.plot(theta, data, 'c.-')
|
138
|
+
ax.fill(theta, data, facecolor='cyan', alpha=0.3)
|
139
|
+
ax.set_varlabels(label)
|
140
|
+
# 標準のグリッド線は円形なので消す(放射方向だけ残す)
|
141
|
+
ax.yaxis.grid(False)
|
142
|
+
# 替わりグリッド線を描く
|
143
|
+
ax.plot(theta, [1]*N, 'k-', marker=None, linewidth=0.5, alpha=0.3)
|
144
|
+
ax.plot(theta, [2]*N, 'k-', marker=None, linewidth=0.5, alpha=0.3)
|
145
|
+
|
146
|
+
plt.show()
|
147
|
+
```
|
148
|
+
|
149
|
+
###やってみたこと
|
150
|
+
|
14
151
|
悪さをしていそうなのが
|
15
152
|
```Python
|
16
153
|
ax.set_varlabels(label)
|
@@ -20,7 +157,6 @@
|
|
20
157
|
|
21
158
|

|
22
159
|
|
23
|
-
何とかして軸ラベルをつけたいと思っております.
|
24
160
|
考えられる可能性や解決方法がございましたら
|
25
161
|
ご教授いただきたいです.
|
26
162
|
|
1
初心者マークの付与
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|