Parallel coordinates plot in plotly

Sample data

Consider the following data for illustration purposes:

import pandas as pd

# Data
df = pd.DataFrame({
    "sepal_length": [5.1, 4.9, 6.3, 6.7, 5.5, 6.0],
    "sepal_width": [3.5, 3.0, 3.3, 3.1, 2.4, 2.9],
    "petal_length": [1.4, 1.4, 6.0, 5.6, 3.8, 4.5],
    "petal_width": [0.2, 0.2, 2.5, 2.4, 1.1, 1.5],
    "species_id": [1, 1, 3, 3, 2, 2]})

Parallel coordinates plot with parallel_coordinates

A parallel coordinates plot represents each observation as a line crossing a set of parallel, vertical axes, one per numeric variable, which makes it easy to compare several variables at once and to spot clusters. Pass a data frame to px.parallel_coordinates and it will use every numeric column by default.

import plotly.express as px

fig = px.parallel_coordinates(df)

fig.show()

Color

Map a numeric column to color (together with color_continuous_scale) to highlight groups of similar observations, for example the species identifier.

import plotly.express as px

fig = px.parallel_coordinates(df, color = "species_id",
                              color_continuous_scale = "viridis")

fig.show()

Selecting the columns

By default every numeric column becomes an axis. Pass a list to dimensions to choose and order them manually, for example to exclude the identifier used for color.

import plotly.express as px

fig = px.parallel_coordinates(
    df, dimensions = ["sepal_length", "sepal_width", "petal_length", "petal_width"],
    color = "species_id", color_continuous_scale = "viridis")

fig.show()

Categorical variant with parallel_categories

If your variables are categorical instead of numeric, use parallel_categories instead, which groups the lines into ribbons for each category rather than drawing individual lines.

import plotly.express as px
import pandas as pd

# Sample data
cat_df = pd.DataFrame({
    "size": ["Small", "Small", "Large", "Large", "Medium"],
    "color": ["Red", "Blue", "Red", "Blue", "Red"],
    "quality": ["Good", "Bad", "Good", "Good", "Bad"]})

fig = px.parallel_categories(cat_df)

fig.show()
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon

See also