Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible parallel_categories magic #2102

Merged
merged 5 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions doc/python/parallel-categories-diagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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](/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

Expand All @@ -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.

Expand Down
1 change: 1 addition & 0 deletions packages/python/plotly/plotly/express/_chart_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = [
Expand Down
6 changes: 6 additions & 0 deletions packages/python/plotly/plotly/express/_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@
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.",
nicolaskruchten marked this conversation as resolved.
Show resolved Hide resolved
"Not used when `dimensions` is passed.",
],
error_x=[
colref_type,
colref_desc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]