plotly

Plotly.Express.Parallel_coordinates

“A parallel coordinate refers to a plot where each point within a given DataFrame is represented as a polyline mark that transverses a set of the parallel axis, each representing a single dimension.
Let us discover how we can create parallel coordinates using Plotly.”

Function Syntax and Parameters

The function syntax is as shown:

plotly.express.parallel_coordinates(data_frame=None, dimensions=None, color=None,

labels=None, color_continuous_scale=None, range_color=None,

color_continuous_midpoint=None, title=None, template=None, width=None,

height=None)

The following are some common parameters you will use when creating parallel coordinates.

  1. data_frame – specifies the data frame containing the columns used in the plot.
  2. dimensions – specifies the values used for multidimensional visualization.
  3. color – specifies the values used to assign unique colors to the marks.
  4. labels – specifies the values used for figure axis title, legend items, and hoves.
  5. title – specifies the title for the plot.
  6. template – defines a template used for the plot.
  7. width/height – sets the figure width and height in pixels.

Example

The example code below shows how to use the iris data to create a parallel coordinate plot.

import plotly.express as px
df = px.data.iris()
fig = px.parallel_coordinates(df, labels={"species_id": "Species", "sepal_width": "Sepal Width", "sepal_length": "Sepal Length"})
fig.show()

The code above will create a parallel coordinate plot of the species_id, sepal_width, and sepal_length columns.

The resulting plot:

We can set a distinct color by setting the color parameter as shown:

import plotly.express as px
df = px.data.iris()
fig = px.parallel_coordinates(df, labels={"species_id": "Species", "sepal_width": "Sepal Width", "sepal_length": "Sepal Length"}, color='species_id')
fig.show()

Output:

You can customize the background color and the lines color scale using the color_continouse_scale and template parameters as shown:

import plotly.express as px
df = px.data.iris()
fig = px.parallel_coordinates(df, labels={"species_id": "Species", "sepal_width": "Sepal Width", "sepal_length": "Sepal Length"}, color='species_id', template='plotly_dark', color_continuous_scale=px.colors.diverging.Armyrose_r)
fig.show()

The resulting figure:

To set the title for the figure, use the title argument:

import plotly.express as px
df = px.data.iris()
fig = px.parallel_coordinates(df, labels={"species_id": "Species", "sepal_width": "Sepal Width", "sepal_length": "Sepal Length"}, color='species_id', template='plotly_dark', color_continuous_scale=px.colors.diverging.Armyrose_r, title='Parallel coordinate for iris data')
fig.show()

Output:

Conclusion

This article shows how to use the Plotly.express.parallel_coordinate() function to create parallel coordinate plots.

Happy coding!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list