From 16b0e676375e026313731f0af58ca7984ca1f036 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Mon, 20 Jan 2020 10:18:58 -0500 Subject: [PATCH 1/5] More flexible parallel_categories magic --- packages/python/plotly/plotly/express/_chart_types.py | 1 + packages/python/plotly/plotly/express/_core.py | 4 +++- packages/python/plotly/plotly/express/_doc.py | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/python/plotly/plotly/express/_chart_types.py b/packages/python/plotly/plotly/express/_chart_types.py index 12cf439e2b3..44e49126e73 100644 --- a/packages/python/plotly/plotly/express/_chart_types.py +++ b/packages/python/plotly/plotly/express/_chart_types.py @@ -1205,6 +1205,7 @@ def parallel_categories( template=None, width=None, height=None, + dimensions_max_cardinality=50, ): """ In a parallel categories (or parallel sets) plot, each row of diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index d1f8854f6df..6bd2037b9c6 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -181,7 +181,9 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): ) and ( trace_spec.constructor != go.Parcats - or len(args["data_frame"][name].unique()) <= 20 + or (v is not None and name in v) + or len(args["data_frame"][name].unique()) + <= args["dimensions_max_cardinality"] ) ] result["dimensions"] = [ diff --git a/packages/python/plotly/plotly/express/_doc.py b/packages/python/plotly/plotly/express/_doc.py index 3a5b9344e19..9ab6b72aaa9 100644 --- a/packages/python/plotly/plotly/express/_doc.py +++ b/packages/python/plotly/plotly/express/_doc.py @@ -106,6 +106,11 @@ colref_list_desc, "Values from these columns are used for multidimensional visualization.", ], + dimensions_max_cardinality=[ + "int (default 50)", + "When `dimensions` is `None` and `data_frame` is provided, " + "columns with more than this number of unique values are excluded from the output.", + ], error_x=[ colref_type, colref_desc, From 74184e0e6fdf7460ab0434de2d87aaf9d21c707d Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Tue, 21 Jan 2020 15:58:05 -0500 Subject: [PATCH 2/5] test dimensions_max_cardinality --- .../test_core/test_px/test_px_functions.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py b/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py index 339accf9d57..bbe8d1d9c6c 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py +++ b/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py @@ -139,3 +139,36 @@ def test_funnel(): color=["0", "0", "0", "1", "1", "1"], ) assert len(fig.data) == 2 + + +def test_parcats_dimensions_max(): + df = px.data.tips() + + # default behaviour + fig = px.parallel_categories(df) + assert [d.label for d in fig.data[0].dimensions] == [ + "sex", + "smoker", + "day", + "time", + "size", + ] + + # explicit subset of default + fig = px.parallel_categories(df, dimensions=["sex", "smoker", "day"]) + assert [d.label for d in fig.data[0].dimensions] == ["sex", "smoker", "day"] + + # shrinking max + fig = px.parallel_categories(df, dimensions_max_cardinality=4) + assert [d.label for d in fig.data[0].dimensions] == [ + "sex", + "smoker", + "day", + "time", + ] + + # explicit superset of default, violating the max + fig = px.parallel_categories( + df, dimensions=["sex", "smoker", "day", "size"], dimensions_max_cardinality=4 + ) + assert [d.label for d in fig.data[0].dimensions] == ["sex", "smoker", "day", "size"] From 33dd9939880c93c155b690d69f79f1d8ebbd04c3 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Tue, 21 Jan 2020 16:17:02 -0500 Subject: [PATCH 3/5] docs for dimensions_max_cardinality --- doc/python/parallel-categories-diagram.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/doc/python/parallel-categories-diagram.md b/doc/python/parallel-categories-diagram.md index c368b5cb49b..a3cedb3eacb 100644 --- a/doc/python/parallel-categories-diagram.md +++ b/doc/python/parallel-categories-diagram.md @@ -5,8 +5,8 @@ jupyter: text_representation: extension: .md format_name: markdown - format_version: "1.1" - jupytext_version: 1.1.1 + format_version: '1.2' + jupytext_version: 1.3.1 kernelspec: display_name: Python 3 language: python @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.7.3 + version: 3.6.8 plotly: description: How to make parallel categories diagrams in Python with Plotly. display_as: statistical @@ -35,16 +35,18 @@ jupyter: #### Parallel Categories Diagram -The parallel categories diagram is a visualization of multi-dimensional categorical data sets. Each variable in the data set is represented by a column of rectangles, where each rectangle corresponds to a discrete value taken on by that variable. The relative heights of the rectangles reflect the relative frequency of occurrence of the corresponding value. +The parallel categories diagram (also known as parallel sets or alluvial diagram) is a visualization of multi-dimensional categorical data sets. Each variable in the data set is represented by a column of rectangles, where each rectangle corresponds to a discrete value taken on by that variable. The relative heights of the rectangles reflect the relative frequency of occurrence of the corresponding value. Combinations of category rectangles across dimensions are connected by ribbons, where the height of the ribbon corresponds to the relative frequency of occurrence of the combination of categories in the data set. -For other representations of multivariate data, also see [parallel coordinates](/python/parallel-coordinates-plot/), [radar charts](/python/radar-chart/) and [scatterplot matrix (SPLOM)](/python/splom/). +For other representations of multivariate data, also see [parallel coordinates](/python/parallel-coordinates-plot/), [radar charts](/python/radar-chart/) and [scatterplot matrix (SPLOM)](/python/splom/). A visually-similar but more generic type of visualization is the [sankey diagrams](https://plot.ly/python/sankey-diagram/). #### Basic Parallel Category Diagram with plotly.express This example visualizes the resturant bills of a sample of 244 people. Hovering over a category rectangle (sex, smoker, etc) displays a tooltip with the number of people with that single trait. Hovering over a ribbon in the diagram displays a tooltip with the number of people with a particular combination of the five traits connected by the ribbon. +By default, `px.parallel_categories` will display any column in the `data_frame` that has a cardinality (or number of unique values) of less than 50. This can be overridden either by passing in a specific list of columns to `dimensions` or by setting `dimensions_max_cardinality` to something other than 50. + ```python import plotly.express as px @@ -68,7 +70,7 @@ fig = px.parallel_categories(df, dimensions=['sex', 'smoker', 'day'], fig.show() ``` -#### Basic Parallel Categories Diagram +### Basic Parallel Categories Diagram with `graph_objects` This example illustartes the hair color, eye color, and sex of a sample of 8 people. The dimension labels can be dragged horizontally to reorder the dimensions and the category rectangles can be dragged vertically to reorder the categories within a dimension. From e6fbcd467701b8561412d92fe01ad66352b57f70 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Tue, 21 Jan 2020 16:20:01 -0500 Subject: [PATCH 4/5] Update doc/python/parallel-categories-diagram.md Co-Authored-By: Emmanuelle Gouillart --- doc/python/parallel-categories-diagram.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/python/parallel-categories-diagram.md b/doc/python/parallel-categories-diagram.md index a3cedb3eacb..58ff71bd587 100644 --- a/doc/python/parallel-categories-diagram.md +++ b/doc/python/parallel-categories-diagram.md @@ -39,7 +39,7 @@ The parallel categories diagram (also known as parallel sets or alluvial diagram Combinations of category rectangles across dimensions are connected by ribbons, where the height of the ribbon corresponds to the relative frequency of occurrence of the combination of categories in the data set. -For other representations of multivariate data, also see [parallel coordinates](/python/parallel-coordinates-plot/), [radar charts](/python/radar-chart/) and [scatterplot matrix (SPLOM)](/python/splom/). A visually-similar but more generic type of visualization is the [sankey diagrams](https://plot.ly/python/sankey-diagram/). +For other representations of multivariate data, also see [parallel coordinates](/python/parallel-coordinates-plot/), [radar charts](/python/radar-chart/) and [scatterplot matrix (SPLOM)](/python/splom/). A visually-similar but more generic type of visualization is the [sankey diagrams](/python/sankey-diagram/). #### Basic Parallel Category Diagram with plotly.express From 83d005f03fb95f77a92eb7bf27c52e65e535b4ed Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Tue, 21 Jan 2020 16:23:34 -0500 Subject: [PATCH 5/5] Update packages/python/plotly/plotly/express/_doc.py Co-Authored-By: Emmanuelle Gouillart --- packages/python/plotly/plotly/express/_doc.py | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/python/plotly/plotly/express/_doc.py b/packages/python/plotly/plotly/express/_doc.py index 9ab6b72aaa9..9e741ef0c8d 100644 --- a/packages/python/plotly/plotly/express/_doc.py +++ b/packages/python/plotly/plotly/express/_doc.py @@ -110,6 +110,7 @@ "int (default 50)", "When `dimensions` is `None` and `data_frame` is provided, " "columns with more than this number of unique values are excluded from the output.", + "Not used when `dimensions` is passed.", ], error_x=[ colref_type,