Skip to content

Commit 756e8d8

Browse files
committed
ENH: Minor change to parallel_coordinates (pandas-dev#15908)
Add option to sort class lables, add to test
1 parent 6c0cfff commit 756e8d8

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pandas/tests/plotting/test_misc.py

+16
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,22 @@ def test_parallel_coordinates(self):
241241
with tm.assert_produces_warning(FutureWarning):
242242
parallel_coordinates(df, 'Name', colors=colors)
243243

244+
df = DataFrame({"feat": [i for i in range(30)],
245+
"class": [2 for _ in range(10)] +
246+
[3 for _ in range(10)] +
247+
[1 for _ in range(10)]})
248+
ax = parallel_coordinates(df, 'class', sort_labels=True)
249+
polylines, lables = ax.get_legend_handles_labels()
250+
color_label_tuples = \
251+
zip([polyline.get_color() for polyline in polylines], labels)
252+
ordered_color_label_tuples = sorted(color_label_tuples,
253+
key=lambda x: x[1])
254+
prev_next_tupels = zip([i for i in ordered_color_label_tuples[0:-1]],
255+
[i for i in ordered_color_label_tuples[1:]])
256+
for prev, nxt in prev_next_tupels:
257+
# lables and colors are ordered strictly increasing
258+
assert prev[1] < nxt[1] and prev[0] < nxt[0]
259+
244260
@slow
245261
def test_radviz(self):
246262
from pandas.tools.plotting import radviz

pandas/tools/plotting.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ def bootstrap_plot(series, fig=None, size=50, samples=500, **kwds):
694694
@deprecate_kwarg(old_arg_name='data', new_arg_name='frame', stacklevel=3)
695695
def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None,
696696
use_columns=False, xticks=None, colormap=None,
697-
axvlines=True, axvlines_kwds=None, **kwds):
697+
axvlines=True, axvlines_kwds=None, sort_labels=False,
698+
**kwds):
698699
"""Parallel coordinates plotting.
699700
700701
Parameters
@@ -718,6 +719,8 @@ def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None,
718719
If true, vertical lines will be added at each xtick
719720
axvlines_kwds: keywords, optional
720721
Options to be passed to axvline method for vertical lines
722+
sort_labels: bool, optional
723+
Sort class_column labels, useful when assigning colours
721724
kwds: keywords
722725
Options to pass to matplotlib plotting method
723726
@@ -774,6 +777,8 @@ def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None,
774777
colormap=colormap, color_type='random',
775778
color=color)
776779

780+
if sort_labels is True:
781+
classes = sorted(classes)
777782
colors = dict(zip(classes, color_values))
778783

779784
for i in range(n):

0 commit comments

Comments
 (0)