You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: How to make parallel categories diagrams in Python with Plotly.
16
26
display_as: statistical
@@ -26,66 +36,64 @@ jupyter:
26
36
title: Python Parallel Categories | Plotly
27
37
---
28
38
29
-
#### New to Plotly?
30
-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
31
-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
32
-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
39
+
#### Parallel Categories Diagram
40
+
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.
33
41
42
+
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.
34
43
35
-
#### Version Check
36
-
Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version.
37
44
38
-
```python
39
-
import plotly
40
-
plotly.__version__
41
-
```
45
+
#### Basic Parallel Category Diagram with plotly.express
46
+
47
+
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.
48
+
42
49
43
50
```python
44
-
from plotly.offline import iplot, init_notebook_mode
45
-
import plotly.graph_objs as go
51
+
import plotly.express as px
46
52
47
-
import pandas as pd
48
-
import numpy as np
49
-
import ipywidgets as widgets
53
+
tips = px.data.tips()
54
+
fig = px.parallel_categories(tips)
55
+
56
+
fig.show()
50
57
```
51
58
52
-
We'll configure the notebook for use in [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode
59
+
#### Style Diagram
60
+
In this example `dimensions` represents a list of stings or the columns of data frame, and `labels` is a dictionary with string keys (column name) and string values ('desired label to be displayed'). See [Plotly express reference page](https://www.plotly.express/plotly_express/#plotly_express.parallel_categories) for more information.
53
61
54
62
```python
55
-
init_notebook_mode(connected=True)
56
-
```
63
+
import plotly.express as px
57
64
58
-
#### Parallel Categories Diagram
59
-
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.
60
-
61
-
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.
In this first example, we visualize the hair color, eye color, and sex of a sample of 8 people. Hovering over a category rectangle 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 three traits connected by the ribbon.
65
-
66
-
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.
73
+
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.
#### Basic Parallel Categories Diagram with Counts
86
92
If the frequency of occurrence for each combination of attributes is known in advance, this can be specified using the `counts` property
87
93
88
94
```python
95
+
import plotly.graph_objects as go
96
+
89
97
parcats = go.Parcats(
90
98
dimensions=[
91
99
{'label': 'Hair',
@@ -97,7 +105,8 @@ parcats = go.Parcats(
97
105
counts=[6, 10, 40, 23, 7]
98
106
)
99
107
100
-
iplot([parcats])
108
+
fig = go.Figure(parcats)
109
+
fig.show()
101
110
```
102
111
103
112
#### Mutli-Color Parallel Categories Diagram
@@ -110,105 +119,76 @@ By setting the `hoveron` property to `'color'` and the `hoverinfo` property to `
110
119
By setting the `arrangement` property to `'freeform'` it is now possible to drag categories horizontally to reorder dimensions as well as vertically to reorder categories within the dimension.
This example demonstrates how the `on_selection` and `on_click` callbacks can be used to implement linked brushing between 3 categorical dimensions displayed with a `parcats` trace and 2 continuous dimensions displayed with a `scatter` trace.
158
156
159
157
This example also sets the `line.shape` property to `hspline` to cause the ribbons to curve between categories.
160
158
161
-
**Note:** In order for the callback functions to be executed the figure must be a `FigureWidget`, and the figure should display itself. In particular the `plot` and `iplot` functions should not be used.
159
+
**Note:** In order for the callback functions to be executed the figure must be a `FigureWidget`, and the figure should display itself.
This example extends the previous example to support brushing with multiple colors. The toggle buttons above may be used to select the active color, and this color will be applied when points are selected in the `scatter` trace and when categories or ribbons are clicked in the `parcats` trace.
0 commit comments