plotly

Plotly.Express.Scatter_matrix

“A scatter matrix refers to a matrix associated with n numerical arrays of the same length.
Let us explore how we can create a scatter matrix with Plotly express.”

Function Syntax and Parameter List

The function takes on syntax as expressed below:

plotly.express.scatter(data_frame=None, x=None, y=None, color=None, symbol=None,

size=None, hover_name=None, hover_data=None, custom_data=None, text=None,

facet_row=None, facet_col=None, facet_col_wrap=0, facet_row_spacing=None,

facet_col_spacing=None, error_x=None, error_x_minus=None, error_y=None,

error_y_minus=None, animation_frame=None, animation_group=None,

category_orders=None, labels=None, orientation=None, color_discrete_sequence=None,

color_discrete_map=None, color_continuous_scale=None, range_color=None,

color_continuous_midpoint=None, symbol_sequence=None, symbol_map=None,

opacity=None, size_max=None, marginal_x=None, marginal_y=None, trendline=None,

trendline_options=None, trendline_color_override=None, trendline_scope='trace',

log_x=False, log_y=False, range_x=None, range_y=None, render_mode='auto',

title=None, template=None, width=None, height=None)

The following outlines the list of most important function parameters you will need to know:

  1. data_frame – specifies the data frame containing the columns used in the plot.
  2. x – specifies the values used to position the marks along the x-axis in the cartesian plane.
  3. y – specifies the values used to position the marks along the y axis.
  4. color – sets the values used to assign a unique color to the marks of the plot.
  5. symbol – defines the values used to assign a symbol to the marks.

Example

The following code creates a basic scatter matrix plot:

import plotly.express as px
df = px.data.iris()
fig = px.scatter_matrix(df)
fig.show()

The code above should return a figure as shown:

To specify the columns, you wish to plot; you can use the dimensions parameter:

import plotly.express as px
df = px.data.iris()
fig = px.scatter_matrix(df, dimensions=['sepal_width', 'sepal_length'])
fig.show()

To assign a color to the scatter marks, you can specify the column to the color parameter as:

import plotly.express as px
from pyparsing import col
df = px.data.iris()
fig = px.scatter_matrix(df, dimensions=['sepal_width', 'sepal_length'], color='species')
fig.show()

The resulting figure is as shown:

To set the title for your scatter matrix plot, you can use the title parameter:

import plotly.express as px
from pyparsing import col
df = px.data.iris()
fig = px.scatter_matrix(df, dimensions=['sepal_width', 'sepal_length'], color='species', title='scatter matrix for iris data')
fig.show()

Output:

To set the width and height of your figure, use their respective parameters and set the desired dimensions in pixels.

import plotly.express as px
from pyparsing import col
df = px.data.iris()
fig = px.scatter_matrix(df, dimensions=['sepal_width', 'sepal_length'], color='species', title='scatter matrix for iris data', width=600, height=400)
fig.show()

Output:

You can also set a discrete color as shown in the code below:

import plotly.express as px
from pyparsing import col
df = px.data.iris()
fig = px.scatter_matrix(df, dimensions=['sepal_width', 'sepal_length'], color='species', title='scatter matrix for iris data', width=600, height=400, color_discrete_sequence=px.colors.sequential.Plasma_r, template='plotly_dark')
fig.show()

Output:

Conclusion

This article covers the various methods and techniques for creating scatter matrix plots using Plotly Express Module.

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