Skip to content

Commit 3fde134

Browse files
stangiralajreback
authored andcommitted
ENH: add option to sort class labels in parallel_coordinates (pandas-dev#15908)
closes pandas-dev#15908 Author: Sarma Tangirala <[email protected]> Closes pandas-dev#15935 from stangirala/master and squashes the following commits: 1467f9f [Sarma Tangirala] Add minor code change, what's new doc fix 3ede37a [Sarma Tangirala] Move feature test to new method, add to whatsnew 756e8d8 [Sarma Tangirala] ENH: Minor change to parallel_coordinates (pandas-dev#15908)
1 parent ebc0c09 commit 3fde134

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ Other Enhancements
373373
- :func:`MultiIndex.remove_unused_levels` has been added to facilitate :ref:`removing unused levels <advanced.shown_levels>`. (:issue:`15694`)
374374
- ``pd.read_csv()`` will now raise a ``ParserError`` error whenever any parsing error occurs (:issue:`15913`, :issue:`15925`)
375375
- ``pd.read_csv()`` now supports the ``error_bad_lines`` and ``warn_bad_lines`` arguments for the Python parser (:issue:`15925`)
376+
- ``parallel_coordinates()`` has gained a ``sort_labels`` keyword arg that sorts class labels and the colours assigned to them (:issue:`15908`)
376377

377378

378379
.. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations

pandas/tests/plotting/test_misc.py

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

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

pandas/tools/plotting.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,8 @@ def bootstrap_plot(series, fig=None, size=50, samples=500, **kwds):
705705
@deprecate_kwarg(old_arg_name='data', new_arg_name='frame', stacklevel=3)
706706
def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None,
707707
use_columns=False, xticks=None, colormap=None,
708-
axvlines=True, axvlines_kwds=None, **kwds):
708+
axvlines=True, axvlines_kwds=None, sort_labels=False,
709+
**kwds):
709710
"""Parallel coordinates plotting.
710711
711712
Parameters
@@ -729,6 +730,11 @@ def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None,
729730
If true, vertical lines will be added at each xtick
730731
axvlines_kwds: keywords, optional
731732
Options to be passed to axvline method for vertical lines
733+
sort_labels: bool, False
734+
Sort class_column labels, useful when assigning colours
735+
736+
.. versionadded:: 0.20.0
737+
732738
kwds: keywords
733739
Options to pass to matplotlib plotting method
734740
@@ -785,6 +791,9 @@ def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None,
785791
colormap=colormap, color_type='random',
786792
color=color)
787793

794+
if sort_labels:
795+
classes = sorted(classes)
796+
color_values = sorted(color_values)
788797
colors = dict(zip(classes, color_values))
789798

790799
for i in range(n):

0 commit comments

Comments
 (0)