@@ -165,44 +165,69 @@ def radviz(frame, class_column, ax=None, **kwds):
165
165
import matplotlib .patches as patches
166
166
import matplotlib .text as text
167
167
import random
168
+
168
169
def random_color (column ):
169
170
random .seed (column )
170
171
return [random .random () for _ in range (3 )]
172
+
171
173
def normalize (series ):
172
174
a = min (series )
173
175
b = max (series )
174
176
return (series - a ) / (b - a )
175
- column_names = [column_name for column_name in frame .columns if column_name != class_column ]
176
- columns = [normalize (frame [column_name ]) for column_name in column_names ]
177
- if ax == None :
177
+
178
+ column_names = [column_name for column_name in frame .columns
179
+ if column_name != class_column ]
180
+
181
+ df = frame [column_names ].apply (normalize )
182
+
183
+ if ax is None :
178
184
ax = plt .gca (xlim = [- 1 , 1 ], ylim = [- 1 , 1 ])
185
+
179
186
classes = set (frame [class_column ])
180
187
to_plot = {}
188
+
181
189
for class_ in classes :
182
190
to_plot [class_ ] = [[], []]
191
+
183
192
n = len (frame .columns ) - 1
184
- s = np .array ([(np .cos (t ), np .sin (t )) for t in [2.0 * np .pi * (i / float (n )) for i in range (n )]])
193
+ s = np .array ([(np .cos (t ), np .sin (t ))
194
+ for t in [2.0 * np .pi * (i / float (n ))
195
+ for i in range (n )]])
196
+
185
197
for i in range (len (frame )):
186
- row = np . array ([ column [ i ] for column in columns ])
198
+ row = df . irow ( i ). values
187
199
row_ = np .repeat (np .expand_dims (row , axis = 1 ), 2 , axis = 1 )
188
200
y = (s * row_ ).sum (axis = 0 ) / row .sum ()
189
- class_name = frame [class_column ][ i ]
201
+ class_name = frame [class_column ]. iget ( i )
190
202
to_plot [class_name ][0 ].append (y [0 ])
191
203
to_plot [class_name ][1 ].append (y [1 ])
204
+
192
205
for class_ in classes :
193
- ax .scatter (to_plot [class_ ][0 ], to_plot [class_ ][1 ], color = random_color (class_ ), label = str (class_ ), ** kwds )
206
+ line = ax .scatter (to_plot [class_ ][0 ],
207
+ to_plot [class_ ][1 ],
208
+ color = random_color (class_ ),
209
+ label = str (class_ ), ** kwds )
210
+ ax .legend ()
211
+
194
212
ax .add_patch (patches .Circle ((0.0 , 0.0 ), radius = 1.0 , facecolor = 'none' ))
213
+
195
214
for xy , name in zip (s , column_names ):
215
+
196
216
ax .add_patch (patches .Circle (xy , radius = 0.025 , facecolor = 'gray' ))
217
+
197
218
if xy [0 ] < 0.0 and xy [1 ] < 0.0 :
198
- ax .text (xy [0 ] - 0.025 , xy [1 ] - 0.025 , name , ha = 'right' , va = 'top' , size = 'small' )
219
+ ax .text (xy [0 ] - 0.025 , xy [1 ] - 0.025 , name ,
220
+ ha = 'right' , va = 'top' , size = 'small' )
199
221
elif xy [0 ] < 0.0 and xy [1 ] >= 0.0 :
200
- ax .text (xy [0 ] - 0.025 , xy [1 ] + 0.025 , name , ha = 'right' , va = 'bottom' , size = 'small' )
222
+ ax .text (xy [0 ] - 0.025 , xy [1 ] + 0.025 , name ,
223
+ ha = 'right' , va = 'bottom' , size = 'small' )
201
224
elif xy [0 ] >= 0.0 and xy [1 ] < 0.0 :
202
- ax .text (xy [0 ] + 0.025 , xy [1 ] - 0.025 , name , ha = 'left' , va = 'top' , size = 'small' )
225
+ ax .text (xy [0 ] + 0.025 , xy [1 ] - 0.025 , name ,
226
+ ha = 'left' , va = 'top' , size = 'small' )
203
227
elif xy [0 ] >= 0.0 and xy [1 ] >= 0.0 :
204
- ax .text (xy [0 ] + 0.025 , xy [1 ] + 0.025 , name , ha = 'left' , va = 'bottom' , size = 'small' )
205
- ax .legend (loc = 'upper right' )
228
+ ax .text (xy [0 ] + 0.025 , xy [1 ] + 0.025 , name ,
229
+ ha = 'left' , va = 'bottom' , size = 'small' )
230
+
206
231
ax .axis ('equal' )
207
232
return ax
208
233
0 commit comments