Skip to content

Commit 5620d1d

Browse files
author
mahdis-z
committed
Parallel category V4
1 parent 5e55bcb commit 5620d1d

File tree

1 file changed

+86
-126
lines changed

1 file changed

+86
-126
lines changed

unconverted/python/parallel-categories-diagram.md renamed to python/parallel-categories-diagram.md

Lines changed: 86 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.1'
9-
jupytext_version: 1.1.1
9+
jupytext_version: 1.2.1
1010
kernelspec:
11-
display_name: Python 2
11+
display_name: Python 3
1212
language: python
13-
name: python2
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.6.8
1424
plotly:
1525
description: How to make parallel categories diagrams in Python with Plotly.
1626
display_as: statistical
@@ -26,66 +36,64 @@ jupyter:
2636
title: Python Parallel Categories | Plotly
2737
---
2838

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.
3341

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.
3443

35-
#### Version Check
36-
Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version.
3744

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+
4249

4350
```python
44-
from plotly.offline import iplot, init_notebook_mode
45-
import plotly.graph_objs as go
51+
import plotly.express as px
4652

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()
5057
```
5158

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.
5361

5462
```python
55-
init_notebook_mode(connected=True)
56-
```
63+
import plotly.express as px
5764

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.
65+
tips = px.data.tips()
66+
fig = px.parallel_categories(tips, dimensions=['sex', 'smoker', 'day'],
67+
color="size", color_continuous_scale=px.colors.sequential.Inferno,
68+
labels={'sex':'SEX', 'smoker':'SMOKER', 'day':'DAY'})
69+
fig.show()
70+
```
6271

6372
#### Basic Parallel Categories Diagram
64-
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.
6774

6875
```python
76+
import plotly.graph_objects as go
6977
parcats = go.Parcats(
7078
dimensions=[
7179
{'label': 'Hair',
72-
'values': ['Black', 'Black', 'Black', 'Brown',
73-
'Brown', 'Brown', 'Red', 'Brown']},
80+
'values': ['Black', 'Black', 'Black', 'Brown', 'Brown', 'Brown', 'Red', 'Brown']},
7481
{'label': 'Eye',
75-
'values': ['Brown', 'Brown', 'Brown', 'Brown',
76-
'Brown', 'Blue', 'Blue', 'Blue']},
82+
'values': ['Brown', 'Brown', 'Brown', 'Brown', 'Brown', 'Blue', 'Blue', 'Blue']},
7783
{'label': 'Sex',
78-
'values': ['Female', 'Female', 'Female', 'Male',
79-
'Female', 'Male', 'Male', 'Male']}]
84+
'values': ['Female', 'Female', 'Female', 'Male', 'Female', 'Male', 'Male', 'Male']}]
8085
)
8186

82-
iplot([parcats])
87+
fig = go.Figure(parcats)
88+
fig.show()
8389
```
8490

8591
#### Basic Parallel Categories Diagram with Counts
8692
If the frequency of occurrence for each combination of attributes is known in advance, this can be specified using the `counts` property
8793

8894
```python
95+
import plotly.graph_objects as go
96+
8997
parcats = go.Parcats(
9098
dimensions=[
9199
{'label': 'Hair',
@@ -97,7 +105,8 @@ parcats = go.Parcats(
97105
counts=[6, 10, 40, 23, 7]
98106
)
99107

100-
iplot([parcats])
108+
fig = go.Figure(parcats)
109+
fig.show()
101110
```
102111

103112
#### Mutli-Color Parallel Categories Diagram
@@ -110,105 +119,76 @@ By setting the `hoveron` property to `'color'` and the `hoverinfo` property to `
110119
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.
111120

112121
```python
113-
titanic_df = pd.read_csv(
114-
"https://raw.githubusercontent.com/plotly/datasets/master/titanic.csv")
122+
import plotly.graph_objects as go
123+
import pandas as pd
124+
125+
titanic_df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/titanic.csv")
115126

116127
# Create dimensions
117128
class_dim = go.parcats.Dimension(
118129
values=titanic_df.Pclass,
119-
categoryorder='category ascending',
120-
label="Class"
130+
categoryorder='category ascending', label="Class"
121131
)
122132

123-
gender_dim = go.parcats.Dimension(
124-
values=titanic_df.Sex,
125-
label="Gender"
126-
)
133+
gender_dim = go.parcats.Dimension(values=titanic_df.Sex, label="Gender")
127134

128135
survival_dim = go.parcats.Dimension(
129-
values=titanic_df.Survived,
130-
label="Outcome",
131-
categoryarray=[0, 1],
132-
ticktext=['perished', 'survived'],
136+
values=titanic_df.Survived, label="Outcome", categoryarray=[0, 1],
137+
ticktext=['perished', 'survived']
133138
)
134139

135140
# Create parcats trace
136141
color = titanic_df.Survived;
137142
colorscale = [[0, 'lightsteelblue'], [1, 'mediumseagreen']];
138143

139-
data = [
140-
go.Parcats(
141-
dimensions=[class_dim, gender_dim, survival_dim],
142-
line={'color': color,
143-
'colorscale': colorscale},
144-
hoveron='color',
145-
hoverinfo='count+probability',
144+
fig = go.Figure(data = [go.Parcats(dimensions=[class_dim, gender_dim, survival_dim],
145+
line={'color': color, 'colorscale': colorscale},
146+
hoveron='color', hoverinfo='count+probability',
146147
labelfont={'size': 18, 'family': 'Times'},
147148
tickfont={'size': 16, 'family': 'Times'},
148-
arrangement='freeform'
149-
)
150-
]
149+
arrangement='freeform')])
151150

152-
# Display figure
153-
iplot(data)
151+
fig.show()
154152
```
155153

156154
#### Parallel Categories Linked Brushing
157155
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.
158156

159157
This example also sets the `line.shape` property to `hspline` to cause the ribbons to curve between categories.
160158

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.
162160

163161
```python
164-
cars_df = pd.read_csv(
165-
'https://raw.githubusercontent.com/plotly/datasets/master/imports-85.csv')
162+
import plotly.graph_objects as go
163+
from ipywidgets import widgets
164+
import pandas as pd
165+
import numpy as np
166+
167+
cars_df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/imports-85.csv')
166168

167169
# Build parcats dimensions
168-
categorical_dimensions = [
169-
'body-style',
170-
'drive-wheels',
171-
'fuel-type'
172-
];
170+
categorical_dimensions = ['body-style', 'drive-wheels', 'fuel-type'];
173171

174-
dimensions = [
175-
dict(values=cars_df[label], label=label)
176-
for label in categorical_dimensions
177-
]
172+
dimensions = [dict(values=cars_df[label], label=label) for label in categorical_dimensions]
178173

179174
# Build colorscale
180175
color = np.zeros(len(cars_df), dtype='uint8')
181176
colorscale = [[0, 'gray'], [1, 'firebrick']]
182177

183178
# Build figure as FigureWidget
184179
fig = go.FigureWidget(
185-
data=[
186-
go.Scatter(
187-
x=cars_df.horsepower,
188-
y=cars_df['highway-mpg'],
189-
marker={'color': 'gray'},
190-
mode='markers',
191-
selected={'marker': {'color': 'firebrick'}},
192-
unselected={'marker': {'opacity': 0.3}}),
193-
194-
go.Parcats(
195-
domain={'y': [0, 0.4]},
196-
dimensions=dimensions,
197-
line={
198-
'colorscale': colorscale,
199-
'cmin': 0,
200-
'cmax': 1,
201-
'color': color,
202-
'shape': 'hspline'})
203-
],
204-
layout=go.Layout(
205-
height=800,
206-
xaxis={'title': 'Horsepower'},
207-
yaxis={'title': 'MPG',
208-
'domain': [0.6, 1]},
209-
dragmode='lasso',
210-
hovermode='closest')
211-
)
180+
data=[go.Scatter(x=cars_df.horsepower, y=cars_df['highway-mpg'],
181+
marker={'color': 'gray'}, mode='markers', selected={'marker': {'color': 'firebrick'}},
182+
unselected={'marker': {'opacity': 0.3}}), go.Parcats(
183+
domain={'y': [0, 0.4]}, dimensions=dimensions,
184+
line={'colorscale': colorscale, 'cmin': 0,
185+
'cmax': 1, 'color': color, 'shape': 'hspline'})
186+
])
187+
188+
fig.update_layout(
189+
height=800, xaxis={'title': 'Horsepower'},
190+
yaxis={'title': 'MPG', 'domain': [0.6, 1]},
191+
dragmode='lasso', hovermode='closest')
212192

213193
# Update color callback
214194
def update_color(trace, points, state):
@@ -225,7 +205,6 @@ fig.data[0].on_selection(update_color)
225205
# and parcats click
226206
fig.data[1].on_click(update_color)
227207

228-
# Display figure
229208
fig
230209
```
231210

@@ -236,8 +215,12 @@ fig
236215
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.
237216

238217
```python
239-
cars_df = pd.read_csv(
240-
'https://raw.githubusercontent.com/plotly/datasets/master/imports-85.csv')
218+
import plotly.graph_objects as go
219+
import ipywidgets as widgets
220+
import pandas as pd
221+
import numpy as np
222+
223+
cars_df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/imports-85.csv')
241224

242225
# Build parcats dimensions
243226
categorical_dimensions = [
@@ -328,27 +311,4 @@ widgets.VBox([color_toggle, fig])
328311

329312

330313
#### Reference
331-
See https://plot.ly/python/reference/#parcats for more information and chart attribute options!
332-
333-
```python
334-
from IPython.display import display, HTML
335-
336-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
337-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
338-
339-
! pip install git+https://github.com/plotly/publisher.git --upgrade
340-
import publisher
341-
publisher.publish(
342-
'parcats.ipynb', 'python/parallel-categories-diagram/', 'Parallel Categories Diagram',
343-
'How to make parallel categories diagrams in Python with Plotly.',
344-
title = 'Python Parallel Categories | Plotly',
345-
has_thumbnail='true', thumbnail='thumbnail/parcats.jpg',
346-
language='python',
347-
display_as='statistical', order=10.3,
348-
uses_plotly_offline=True,
349-
ipynb= '~notebook_demo/258')
350-
```
351-
352-
```python
353-
354-
```
314+
See [reference page](https://plot.ly/python/reference/#parcats) for more information and chart attribute options!

0 commit comments

Comments
 (0)