Skip to content

Commit 9c6eadf

Browse files
committed
BUG: tweaks to radviz method, add integer indexing docs close #1607
1 parent d4274d5 commit 9c6eadf

File tree

4 files changed

+72
-17
lines changed

4 files changed

+72
-17
lines changed

doc/data/iris.data

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,4 @@ SepalLength,SepalWidth,PetalLength,PetalWidth,Name
148148
6.3,2.5,5.0,1.9,Iris-virginica
149149
6.5,3.0,5.2,2.0,Iris-virginica
150150
6.2,3.4,5.4,2.3,Iris-virginica
151-
5.9,3.0,5.1,1.8,Iris-virginica
152-
151+
5.9,3.0,5.1,1.8,Iris-virginica

doc/source/gotchas.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ integer arrays to floating when NAs must be introduced.
118118
Integer indexing
119119
----------------
120120

121+
Label-based indexing with integer axis labels is a thorny topic. It has been
122+
discussed heavily on mailing lists and among various members of the scientific
123+
Python community. In pandas, our general viewpoint is that labels matter more
124+
than integer locations. Therefore, with an integer axis index *only*
125+
label-based indexing is possible with the standard tools like ``.ix``. The
126+
following code will generate exceptions:
127+
128+
.. code-block:: python
129+
130+
s = Series(range(5))
131+
s[-1]
132+
df = DataFrame(np.random.randn(5, 4))
133+
df
134+
df.ix[-2:]
135+
136+
This deliberate decision was made to prevent ambiguities and subtle bugs (many
137+
users reported finding bugs when the API change was made to stop "falling back"
138+
on position-based indexing).
139+
121140
Label-based slicing conventions
122141
-------------------------------
123142

doc/source/indexing.rst

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,24 @@ numpy array. For instance,
475475
476476
Advanced indexing with integer labels
477477
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
478-
479478
Label-based indexing with integer axis labels is a thorny topic. It has been
480479
discussed heavily on mailing lists and among various members of the scientific
481480
Python community. In pandas, our general viewpoint is that labels matter more
482-
than integer locations. Therefore, advanced indexing with ``.ix`` will always
483-
attempt label-based indexing, before falling back on integer-based indexing.
481+
than integer locations. Therefore, with an integer axis index *only*
482+
label-based indexing is possible with the standard tools like ``.ix``. The
483+
following code will generate exceptions:
484+
485+
.. code-block:: python
486+
487+
s = Series(range(5))
488+
s[-1]
489+
df = DataFrame(np.random.randn(5, 4))
490+
df
491+
df.ix[-2:]
492+
493+
This deliberate decision was made to prevent ambiguities and subtle bugs (many
494+
users reported finding bugs when the API change was made to stop "falling back"
495+
on position-based indexing).
484496

485497
Setting values in mixed-type DataFrame
486498
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pandas/tools/plotting.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,44 +165,69 @@ def radviz(frame, class_column, ax=None, **kwds):
165165
import matplotlib.patches as patches
166166
import matplotlib.text as text
167167
import random
168+
168169
def random_color(column):
169170
random.seed(column)
170171
return [random.random() for _ in range(3)]
172+
171173
def normalize(series):
172174
a = min(series)
173175
b = max(series)
174176
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:
178184
ax = plt.gca(xlim=[-1, 1], ylim=[-1, 1])
185+
179186
classes = set(frame[class_column])
180187
to_plot = {}
188+
181189
for class_ in classes:
182190
to_plot[class_] = [[], []]
191+
183192
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+
185197
for i in range(len(frame)):
186-
row = np.array([column[i] for column in columns])
198+
row = df.irow(i).values
187199
row_ = np.repeat(np.expand_dims(row, axis=1), 2, axis=1)
188200
y = (s * row_).sum(axis=0) / row.sum()
189-
class_name = frame[class_column][i]
201+
class_name = frame[class_column].iget(i)
190202
to_plot[class_name][0].append(y[0])
191203
to_plot[class_name][1].append(y[1])
204+
192205
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+
194212
ax.add_patch(patches.Circle((0.0, 0.0), radius=1.0, facecolor='none'))
213+
195214
for xy, name in zip(s, column_names):
215+
196216
ax.add_patch(patches.Circle(xy, radius=0.025, facecolor='gray'))
217+
197218
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')
199221
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')
201224
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')
203227
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+
206231
ax.axis('equal')
207232
return ax
208233

0 commit comments

Comments
 (0)