ScatterMatrix
A scatter chart for every pair of dimensions, distributions on the diagonal. The Scatter Matrix guide shows every feature on real data; this page is the contract: the function, the shape of its data, the keys style takes, and the constant each parameter accepts.
Function
datachart.charts.ScatterMatrix
ScatterMatrix(
data: (
dict[str, list[Any]]
| list[ScatterMatrixDataPointAttrs]
),
*,
dimensions: list[str] | None = None,
hue: str | None = None,
diagonal: SCATTER_MATRIX_DIAGONAL | str | None = None,
lower_only: bool | None = None,
show_regression: bool | None = None,
show_correlation: bool | None = None,
sharex: bool | None = None,
sharey: bool | None = None,
title: str | None = None,
figsize: tuple[float, float] | None = None,
show_legend: bool | None = None,
legend: LegendSettingAttrs | None = None,
show_grid: SHOW_GRID | str | bool | None = None,
style: StyleAttrs | None = None
) -> plt.Figure
Creates a scatter matrix.
Every pair of numeric dimensions gets a scatter chart, and each
dimension's own distribution sits on the diagonal. Use it to scan many
variables for relationships, clusters and outliers at once, optionally
split by a categorical hue. For two variables use
ScatterChart; for many dimensions
per observation read as lines, use
ParallelCoords.
The figure is a grid: it nests inside
Grid and cannot be overlaid with
Panel.
Examples:
>>> from datachart.charts import ScatterMatrix
>>> figure = ScatterMatrix(
... data={
... "length": [5.1, 4.9, 6.3, 5.8, 7.1, 6.5],
... "width": [3.5, 3.0, 3.3, 2.7, 3.0, 3.2],
... "petal": [1.4, 1.4, 6.0, 5.1, 5.9, 5.1],
... "species": ["a", "a", "b", "b", "b", "b"],
... },
... hue="species",
... )
>>>
>>> # records work too; correlations above the diagonal
>>> records = [
... {"length": 5.1, "width": 3.5, "petal": 1.4},
... {"length": 6.3, "width": 3.3, "petal": 6.0},
... {"length": 5.8, "width": 2.7, "petal": 5.1},
... ]
>>> figure = ScatterMatrix(
... data=records,
... diagonal="kde",
... show_correlation=True,
... show_regression=True,
... )
| PARAMETER | DESCRIPTION |
|---|---|
data
|
The observations: a dict of equal-length columns, or a list of
records. Missing values (
TYPE:
|
dimensions
|
The numeric columns to plot, in order. Defaults to every
numeric column except the
TYPE:
|
hue
|
The column whose categories colour the points, one colour per category and one legend for the whole figure. A numeric column raises.
TYPE:
|
diagonal
|
What each dimension's own cell shows: a histogram (
TYPE:
|
lower_only
|
Whether to leave the cells above the diagonal empty.
Wins over
TYPE:
|
show_regression
|
Whether to draw a least-squares line per hue group in every scatter cell.
TYPE:
|
show_correlation
|
Whether to replace the scatters above the diagonal with the Pearson correlation of each hue group.
TYPE:
|
sharex
|
Whether the cells of a column share one x-axis and only
the bottom row labels its ticks (default
TYPE:
|
sharey
|
Whether the cells of a row share one y-axis and only the
left column labels its ticks (default
TYPE:
|
title
|
The title of the figure.
TYPE:
|
figsize
|
The size of the figure; the cells stay square inside it. Defaults to 2.2 inches per cell, shrunk so the figure is at most 6.3 inches (a full page width) wide.
TYPE:
|
show_legend
|
Whether to show the legend of the hue groups (default
TYPE:
|
legend
|
The legend setting: title, column count, alignment and
location. The title defaults to the
TYPE:
|
show_grid
|
Which grid lines to show in the cells (e.g., "both", "x",
"y");
TYPE:
|
style
|
Style attributes for every cell: the scatter, histogram,
plot text and
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
plt.Figure
|
The figure containing the scatter matrix. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Data
Each record in data is a ScatterMatrixDataPointAttrs; the hue parameter renames its keys.
datachart.typings.ScatterMatrixDataPointAttrs
Bases: TypedDict
The record attributes for the scatter matrix.
A dictionary where keys are column names: numeric columns become
dimensions, and one categorical column may be named as the hue. The
same columns can be passed as one dictionary of lists instead.
| ATTRIBUTE | DESCRIPTION |
|---|---|
hue |
The category for color grouping, under the
column name the
TYPE:
|
Style
style takes the keys of StyleAttrs. Its own keys are those of ScatterMatrixStyleAttrs. The chart also reads the shared groups it draws: the regression line (RegressionStyleAttrs). Every key falls back to the theme, so the same keys set the default look through config.
datachart.typings.ScatterMatrixStyleAttrs
Bases: TypedDict
The typing for the scatter matrix style.
The cells take the scatter, histogram and plot text keys; these keys style what the matrix adds on top of them.
| ATTRIBUTE | DESCRIPTION |
|---|---|
plot_scatter_matrix_regression_color |
The color of the regression lines under
TYPE:
|
plot_scatter_matrix_regression_width |
The line width of the regression lines.
TYPE:
|
plot_scatter_matrix_regression_style |
The line style of the regression lines.
TYPE:
|
plot_scatter_matrix_correlation_size |
The font size of the correlation text under
TYPE:
|
plot_scatter_matrix_correlation_weight |
The font weight of the correlation text.
TYPE:
|
plot_scatter_matrix_kde_width |
The line width of the diagonal density curves.
TYPE:
|
plot_scatter_matrix_kde_alpha |
The alpha value of the fill under the diagonal density curves; 0 draws no fill.
TYPE:
|
plot_scatter_matrix_diagonal_alpha |
The alpha value of the diagonal histograms, overlaid per hue group.
TYPE:
|
Constants
The parameters that accept a constant, with the class in datachart.constants that lists its values.
| Parameter | Constant |
|---|---|
diagonal |
SCATTER_MATRIX_DIAGONAL |
legend={"location": ..., "alignment": ...} |
LEGEND_LOCATION, LEGEND_ALIGN |
show_grid |
SHOW_GRID |