{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"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/).\n",
"
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).\n",
"
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Version Check\n",
"Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3.4.0rc1'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import plotly.graph_objs as go\n",
"\n",
"import pandas as pd\n",
"import numpy as np\n",
"import ipywidgets as widgets"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Parallel Categories Trace\n",
"The parallel categories trace 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.\n",
"\n",
"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.\n",
"\n",
"#### Basic Parallel Categories Trace\n",
"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.\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "289c067216404b5a8cc75a327707af16",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"FigureWidget({\n",
" 'data': [{'dimensions': [{'label': 'Hair',\n",
" 'values': [Black, …"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"parcats = go.Parcats(\n",
" dimensions=[\n",
" {'label': 'Hair',\n",
" 'values': ['Black', 'Black', 'Black', 'Brown',\n",
" 'Brown', 'Brown', 'Red', 'Brown']},\n",
" {'label': 'Eye',\n",
" 'values': ['Brown', 'Brown', 'Brown', 'Brown',\n",
" 'Brown', 'Blue', 'Blue', 'Blue']},\n",
" {'label': 'Sex',\n",
" 'values': ['Female', 'Female', 'Female', 'Male',\n",
" 'Female', 'Male', 'Male', 'Male']}]\n",
")\n",
"\n",
"go.FigureWidget(data=[parcats], layout={'width': 800})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Basic Parallel Categories Trace with Counts\n",
"If the frequency of occurrence for each combination of attributes is known in advance, this can be specified using the `counts` property"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6ba931d1e8e1415cbffe326c888f4c4b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"FigureWidget({\n",
" 'data': [{'counts': [6, 10, 40, 23, 7],\n",
" 'dimensions': [{'label': 'Hair', 'val…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"parcats = go.Parcats(\n",
" dimensions=[\n",
" {'label': 'Hair',\n",
" 'values': ['Black', 'Brown', 'Brown', 'Brown', 'Red']},\n",
" {'label': 'Eye',\n",
" 'values': ['Brown', 'Brown', 'Brown', 'Blue', 'Blue']},\n",
" {'label': 'Sex',\n",
" 'values': ['Female', 'Male', 'Female', 'Male', 'Male']}],\n",
" counts=[6, 10, 40, 23, 7]\n",
")\n",
"\n",
"go.FigureWidget(data=[parcats], layout={'width': 800})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Mutli-Color Parallel Categories Trace\n",
"The color of the ribbons can be specified with the `line.color` property. Similar to other trace types, this property may be set to an array of numbers, which are then mapped to colors according to the the colorscale specified in the `line.colorscale` property.\n",
"\n",
"Here is an example of visualizing the survival rate of passengers in the titanic dataset, where the ribbons are colored based on survival outcome.\n",
"\n",
"By setting the `hoveron` property to `'color'` and the `hoverinfo` property to `'count+probability'` the tooltips now display count and probability information for each color (outcome) per category.\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n", " | PassengerId | \n", "Survived | \n", "Pclass | \n", "Name | \n", "Sex | \n", "Age | \n", "SibSp | \n", "Parch | \n", "Ticket | \n", "Fare | \n", "Cabin | \n", "Embarked | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "1 | \n", "0 | \n", "3 | \n", "Braund, Mr. Owen Harris | \n", "male | \n", "22.0 | \n", "1 | \n", "0 | \n", "A/5 21171 | \n", "7.2500 | \n", "NaN | \n", "S | \n", "
1 | \n", "2 | \n", "1 | \n", "1 | \n", "Cumings, Mrs. John Bradley (Florence Briggs Th... | \n", "female | \n", "38.0 | \n", "1 | \n", "0 | \n", "PC 17599 | \n", "71.2833 | \n", "C85 | \n", "C | \n", "
2 | \n", "3 | \n", "1 | \n", "3 | \n", "Heikkinen, Miss. Laina | \n", "female | \n", "26.0 | \n", "0 | \n", "0 | \n", "STON/O2. 3101282 | \n", "7.9250 | \n", "NaN | \n", "S | \n", "
3 | \n", "4 | \n", "1 | \n", "1 | \n", "Futrelle, Mrs. Jacques Heath (Lily May Peel) | \n", "female | \n", "35.0 | \n", "1 | \n", "0 | \n", "113803 | \n", "53.1000 | \n", "C123 | \n", "S | \n", "
4 | \n", "5 | \n", "0 | \n", "3 | \n", "Allen, Mr. William Henry | \n", "male | \n", "35.0 | \n", "0 | \n", "0 | \n", "373450 | \n", "8.0500 | \n", "NaN | \n", "S | \n", "
\n", " | symboling | \n", "normalized-losses | \n", "make | \n", "fuel-type | \n", "aspiration | \n", "num-of-doors | \n", "body-style | \n", "drive-wheels | \n", "engine-location | \n", "wheel-base | \n", "... | \n", "engine-size | \n", "fuel-system | \n", "bore | \n", "stroke | \n", "compression-ratio | \n", "horsepower | \n", "peak-rpm | \n", "city-mpg | \n", "highway-mpg | \n", "price | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "3 | \n", "NaN | \n", "alfa-romero | \n", "gas | \n", "std | \n", "two | \n", "convertible | \n", "rwd | \n", "front | \n", "88.6 | \n", "... | \n", "130 | \n", "mpfi | \n", "3.47 | \n", "2.68 | \n", "9.0 | \n", "111.0 | \n", "5000.0 | \n", "21 | \n", "27 | \n", "13495.0 | \n", "
1 | \n", "3 | \n", "NaN | \n", "alfa-romero | \n", "gas | \n", "std | \n", "two | \n", "convertible | \n", "rwd | \n", "front | \n", "88.6 | \n", "... | \n", "130 | \n", "mpfi | \n", "3.47 | \n", "2.68 | \n", "9.0 | \n", "111.0 | \n", "5000.0 | \n", "21 | \n", "27 | \n", "16500.0 | \n", "
2 | \n", "1 | \n", "NaN | \n", "alfa-romero | \n", "gas | \n", "std | \n", "two | \n", "hatchback | \n", "rwd | \n", "front | \n", "94.5 | \n", "... | \n", "152 | \n", "mpfi | \n", "2.68 | \n", "3.47 | \n", "9.0 | \n", "154.0 | \n", "5000.0 | \n", "19 | \n", "26 | \n", "16500.0 | \n", "
3 | \n", "2 | \n", "164.0 | \n", "audi | \n", "gas | \n", "std | \n", "four | \n", "sedan | \n", "fwd | \n", "front | \n", "99.8 | \n", "... | \n", "109 | \n", "mpfi | \n", "3.19 | \n", "3.40 | \n", "10.0 | \n", "102.0 | \n", "5500.0 | \n", "24 | \n", "30 | \n", "13950.0 | \n", "
4 | \n", "2 | \n", "164.0 | \n", "audi | \n", "gas | \n", "std | \n", "four | \n", "sedan | \n", "4wd | \n", "front | \n", "99.4 | \n", "... | \n", "136 | \n", "mpfi | \n", "3.19 | \n", "3.40 | \n", "8.0 | \n", "115.0 | \n", "5500.0 | \n", "18 | \n", "22 | \n", "17450.0 | \n", "
5 rows × 26 columns
\n", "