From 756e8d8f53c4b75d33f0a3f6ea496a1ab97af586 Mon Sep 17 00:00:00 2001 From: Sarma Tangirala Date: Thu, 6 Apr 2017 21:51:07 -0700 Subject: [PATCH 1/3] ENH: Minor change to parallel_coordinates (#15908) Add option to sort class lables, add to test --- pandas/tests/plotting/test_misc.py | 16 ++++++++++++++++ pandas/tools/plotting.py | 7 ++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index 812f039f1a2c7..f7c8de0959496 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -241,6 +241,22 @@ def test_parallel_coordinates(self): with tm.assert_produces_warning(FutureWarning): parallel_coordinates(df, 'Name', colors=colors) + df = DataFrame({"feat": [i for i in range(30)], + "class": [2 for _ in range(10)] + + [3 for _ in range(10)] + + [1 for _ in range(10)]}) + ax = parallel_coordinates(df, 'class', sort_labels=True) + polylines, lables = ax.get_legend_handles_labels() + color_label_tuples = \ + zip([polyline.get_color() for polyline in polylines], labels) + ordered_color_label_tuples = sorted(color_label_tuples, + key=lambda x: x[1]) + prev_next_tupels = zip([i for i in ordered_color_label_tuples[0:-1]], + [i for i in ordered_color_label_tuples[1:]]) + for prev, nxt in prev_next_tupels: + # lables and colors are ordered strictly increasing + assert prev[1] < nxt[1] and prev[0] < nxt[0] + @slow def test_radviz(self): from pandas.tools.plotting import radviz diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index f70a2b0b22140..9270b2f452e0a 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -694,7 +694,8 @@ def bootstrap_plot(series, fig=None, size=50, samples=500, **kwds): @deprecate_kwarg(old_arg_name='data', new_arg_name='frame', stacklevel=3) def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None, use_columns=False, xticks=None, colormap=None, - axvlines=True, axvlines_kwds=None, **kwds): + axvlines=True, axvlines_kwds=None, sort_labels=False, + **kwds): """Parallel coordinates plotting. Parameters @@ -718,6 +719,8 @@ def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None, If true, vertical lines will be added at each xtick axvlines_kwds: keywords, optional Options to be passed to axvline method for vertical lines + sort_labels: bool, optional + Sort class_column labels, useful when assigning colours kwds: keywords Options to pass to matplotlib plotting method @@ -774,6 +777,8 @@ def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None, colormap=colormap, color_type='random', color=color) + if sort_labels is True: + classes = sorted(classes) colors = dict(zip(classes, color_values)) for i in range(n): From 3ede37afeb675f61b604bc49c7d9e29617678ff3 Mon Sep 17 00:00:00 2001 From: Sarma Tangirala Date: Fri, 7 Apr 2017 12:14:23 -0700 Subject: [PATCH 2/3] Move feature test to new method, add to whatsnew Also fix minor bug due to a misnamed var that both the test and pyflakes missed --- doc/source/whatsnew/v0.20.0.txt | 2 ++ pandas/tests/plotting/test_misc.py | 6 +++++- pandas/tools/plotting.py | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index fd1cd3d0022c9..050393c8c30b5 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -371,6 +371,8 @@ Other Enhancements - :func:`MultiIndex.remove_unused_levels` has been added to facilitate :ref:`removing unused levels `. (:issue:`15694`) - ``pd.read_csv()`` will now raise a ``ParserError`` error whenever any parsing error occurs (:issue:`15913`, :issue:`15925`) - ``pd.read_csv()`` now supports the ``error_bad_lines`` and ``warn_bad_lines`` arguments for the Python parser (:issue:`15925`) +- ``pd.read_csv()`` will now raise a ``csv.Error`` error whenever an end-of-file character is encountered in the middle of a data row (:issue:`15913`) +- ``parallel_coordinates()`` now has a ``sort_labels`` keyword arg that sorts class labels and the colours assigned to them (:issue:`15908`) .. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index f7c8de0959496..504c55bcfcfd0 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -241,12 +241,16 @@ def test_parallel_coordinates(self): with tm.assert_produces_warning(FutureWarning): parallel_coordinates(df, 'Name', colors=colors) + def test_parallel_coordinates_with_sorted_labels(self): + """ For #15908 """ + from pandas.tools.plotting import parallel_coordinates + df = DataFrame({"feat": [i for i in range(30)], "class": [2 for _ in range(10)] + [3 for _ in range(10)] + [1 for _ in range(10)]}) ax = parallel_coordinates(df, 'class', sort_labels=True) - polylines, lables = ax.get_legend_handles_labels() + polylines, labels = ax.get_legend_handles_labels() color_label_tuples = \ zip([polyline.get_color() for polyline in polylines], labels) ordered_color_label_tuples = sorted(color_label_tuples, diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 9270b2f452e0a..9114d05afb77a 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -719,8 +719,11 @@ def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None, If true, vertical lines will be added at each xtick axvlines_kwds: keywords, optional Options to be passed to axvline method for vertical lines - sort_labels: bool, optional + sort_labels: bool, False Sort class_column labels, useful when assigning colours + + .. versionadded:: 0.20.0 + kwds: keywords Options to pass to matplotlib plotting method @@ -779,6 +782,7 @@ def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None, if sort_labels is True: classes = sorted(classes) + color_values = sorted(color_values) colors = dict(zip(classes, color_values)) for i in range(n): From 1467f9fa6147e8451059021a37d7ccba4b0556fe Mon Sep 17 00:00:00 2001 From: Sarma Tangirala Date: Fri, 14 Apr 2017 02:20:40 -0700 Subject: [PATCH 3/3] Add minor code change, what's new doc fix --- doc/source/whatsnew/v0.20.0.txt | 2 +- pandas/tools/plotting.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 050393c8c30b5..382e57e0421c5 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -372,7 +372,7 @@ Other Enhancements - ``pd.read_csv()`` will now raise a ``ParserError`` error whenever any parsing error occurs (:issue:`15913`, :issue:`15925`) - ``pd.read_csv()`` now supports the ``error_bad_lines`` and ``warn_bad_lines`` arguments for the Python parser (:issue:`15925`) - ``pd.read_csv()`` will now raise a ``csv.Error`` error whenever an end-of-file character is encountered in the middle of a data row (:issue:`15913`) -- ``parallel_coordinates()`` now has a ``sort_labels`` keyword arg that sorts class labels and the colours assigned to them (:issue:`15908`) +- ``parallel_coordinates()`` has gained a ``sort_labels`` keyword arg that sorts class labels and the colours assigned to them (:issue:`15908`) .. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 9114d05afb77a..b15ccdacb6ab7 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -780,7 +780,7 @@ def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None, colormap=colormap, color_type='random', color=color) - if sort_labels is True: + if sort_labels: classes = sorted(classes) color_values = sorted(color_values) colors = dict(zip(classes, color_values))