前提・実現したいこと
Python3系でレーダーチャートを作成したく
以下のサイトを参考に作成しておりました.
https://matplotlib.org/examples/api/radar_chart.html
https://teratail.com/questions/104066
しかしラベルをつけようとレイアウトが崩れてしまう問題が発生いたしました.
私の結果は以下のようになってしまいます.
###コード
使用したコードは以下になります.
コピペコードですので元のサイトを一緒に掲載させていただきます.
まずhttps://matplotlib.org/examples/api/radar_chart.htmlより
実際にレーダーチャートを作成する関数をお借りしました.
Python
1def radar_factory(num_vars, frame='circle'): 2 """Create a radar chart with `num_vars` axes. 3 4 This function creates a RadarAxes projection and registers it. 5 6 Parameters 7 ---------- 8 num_vars : int 9 Number of variables for radar chart. 10 frame : {'circle' | 'polygon'} 11 Shape of frame surrounding axes. 12 13 """ 14 # calculate evenly-spaced axis angles 15 theta = np.linspace(0, 2*np.pi, num_vars, endpoint=False) 16 # rotate theta such that the first axis is at the top 17 theta += np.pi/2 18 19 def draw_poly_patch(self): 20 verts = unit_poly_verts(theta) 21 return plt.Polygon(verts, closed=True, edgecolor='k') 22 23 def draw_circle_patch(self): 24 # unit circle centered on (0.5, 0.5) 25 return plt.Circle((0.5, 0.5), 0.5) 26 27 patch_dict = {'polygon': draw_poly_patch, 'circle': draw_circle_patch} 28 if frame not in patch_dict: 29 raise ValueError('unknown value for `frame`: %s' % frame) 30 31 class RadarAxes(PolarAxes): 32 33 name = 'radar' 34 # use 1 line segment to connect specified points 35 RESOLUTION = 1 36 # define draw_frame method 37 draw_patch = patch_dict[frame] 38 39 def fill(self, *args, **kwargs): 40 """Override fill so that line is closed by default""" 41 closed = kwargs.pop('closed', True) 42 return super(RadarAxes, self).fill(closed=closed, *args, **kwargs) 43 44 def plot(self, *args, **kwargs): 45 """Override plot so that line is closed by default""" 46 lines = super(RadarAxes, self).plot(*args, **kwargs) 47 for line in lines: 48 self._close_line(line) 49 50 def _close_line(self, line): 51 x, y = line.get_data() 52 # FIXME: markers at x[0], y[0] get doubled-up 53 if x[0] != x[-1]: 54 x = np.concatenate((x, [x[0]])) 55 y = np.concatenate((y, [y[0]])) 56 line.set_data(x, y) 57 58 def set_varlabels(self, labels): 59 self.set_thetagrids(np.degrees(theta), labels) 60 61 def _gen_axes_patch(self): 62 return self.draw_patch() 63 64 def _gen_axes_spines(self): 65 if frame == 'circle': 66 return PolarAxes._gen_axes_spines(self) 67 # The following is a hack to get the spines (i.e. the axes frame) 68 # to draw correctly for a polygon frame. 69 70 # spine_type must be 'left', 'right', 'top', 'bottom', or `circle`. 71 spine_type = 'circle' 72 verts = unit_poly_verts(theta) 73 # close off polygon by repeating first vertex 74 verts.append(verts[0]) 75 path = Path(verts) 76 77 spine = Spine(self, spine_type, path) 78 spine.set_transform(self.transAxes) 79 return {'polar': spine} 80 81 register_projection(RadarAxes) 82 return theta 83 84 85def unit_poly_verts(theta): 86 """Return vertices of polygon for subplot axes. 87 88 This polygon is circumscribed by a unit circle centered at (0.5, 0.5) 89 """ 90 x0, y0, r = [0.5] * 3 91 verts = [(r*np.cos(t) + x0, r*np.sin(t) + y0) for t in theta] 92 return verts
その後,magichan様によるコード(掲載元→https://teratail.com/questions/104066)によって整形いたしました.
Python
1N = 9 2theta = radar_factory(N, frame='polygon') 3 4label= ['A','B','C','D','E','F','G','H','I'] 5data = [3.0, 0.00, 2.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00] 6 7ax = plt.subplot(projection='radar') 8# chartの範囲を0-3 9ax.set_ylim(0, 3) 10# Grid線を位置の指定 11ax.set_rgrids([1, 2, 3]) 12# 描画処理 13ax.plot(theta, data, 'c.-') 14ax.fill(theta, data, facecolor='cyan', alpha=0.3) 15ax.set_varlabels(label) 16# 標準のグリッド線は円形なので消す(放射方向だけ残す) 17ax.yaxis.grid(False) 18# 替わりグリッド線を描く 19ax.plot(theta, [1]*N, 'k-', marker=None, linewidth=0.5, alpha=0.3) 20ax.plot(theta, [2]*N, 'k-', marker=None, linewidth=0.5, alpha=0.3) 21 22plt.show()
###やってみたこと
原因となっていそうなのが
Python
1ax.set_varlabels(label)
だと考えられます.
こちらをコメントアウトすると以下のようなレーダーチャートができます.
考えられる可能性や解決方法がございましたら
ご教授いただきたいです.
初めて質問させていただいております.
不慣れな点もあるかと思いますが
どうかよろしくおねがいいたします.
回答2件
あなたの回答
tips
プレビュー