ScatterChart
One point per observation, placed by two numeric variables. The Scatter Chart 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.ScatterChart
ScatterChart(
data: (
list[ScatterDataPointAttrs]
| list[list[ScatterDataPointAttrs]]
),
*,
title: str | None = None,
xlabel: str | None = None,
ylabel: str | None = None,
subtitle: str | list[str | None] | None = None,
emphasis: (
EMPHASIS | str | list[str | None] | None
) = None,
emphasis_rule: EmphasisRuleAttrs | None = None,
figsize: FIG_SIZE | tuple[float, float] | None = None,
xmin: int | float | datetime | None = None,
xmax: int | float | datetime | None = None,
ymin: int | float | None = None,
ymax: int | float | None = None,
show_legend: bool | None = None,
legend: LegendSettingAttrs | None = None,
show_grid: SHOW_GRID | str | bool | None = None,
show_regression: bool | None = None,
show_ci: bool | None = None,
ci_level: float | None = None,
show_correlation: bool | None = None,
show_values: bool | None = None,
value_format: VALUE_FORMAT | str | None = None,
value_step: int | None = None,
aspect_ratio: ASPECT_RATIO | str | None = None,
scalex: SCALE | str | None = None,
scaley: SCALE | str | None = None,
subplots: bool | None = None,
max_cols: int | None = None,
sharex: bool | None = None,
sharey: bool | None = None,
style: (
ScatterStyleAttrs
| list[ScatterStyleAttrs | None]
| None
) = None,
xticks: (
list[int | float | datetime]
| list[list[int | float | datetime]]
| None
) = None,
xticklabels: list[str] | list[list[str]] | None = None,
xtickrotate: int | list[int | None] | None = None,
yticks: (
list[int | float] | list[list[int | float]] | None
) = None,
yticklabels: list[str] | list[list[str]] | None = None,
ytickrotate: int | list[int | None] | None = None,
xticks_format: (
VALUE_FORMAT | DATE_FORMAT | str | None
) = None,
yticks_format: (
VALUE_FORMAT | DATE_FORMAT | str | None
) = None,
vlines: (
VLineSettingAttrs
| list[VLineSettingAttrs]
| list[
VLineSettingAttrs
| list[VLineSettingAttrs]
| None
]
| None
) = None,
hlines: (
HLineSettingAttrs
| list[HLineSettingAttrs]
| list[
HLineSettingAttrs
| list[HLineSettingAttrs]
| None
]
| None
) = None,
vspans: (
VSpanSettingAttrs
| list[VSpanSettingAttrs]
| list[
VSpanSettingAttrs
| list[VSpanSettingAttrs]
| None
]
| None
) = None,
hspans: (
HSpanSettingAttrs
| list[HSpanSettingAttrs]
| list[
HSpanSettingAttrs
| list[HSpanSettingAttrs]
| None
]
| None
) = None,
texts: (
TextSettingAttrs
| list[TextSettingAttrs]
| list[
TextSettingAttrs | list[TextSettingAttrs] | None
]
| None
) = None,
x: str | list[str | None] | None = None,
y: str | list[str | None] | None = None,
size: str | list[str | None] | None = None,
hue: str | list[str | None] | None = None,
label: str | list[str | None] | None = None,
size_range: tuple[float, float] | None = None
) -> plt.Figure
Creates a scatter chart.
Each point is one observation placed by two numeric variables, optionally
with a third encoded as marker size. Use it to check whether two variables
are related, spot clusters and outliers, and quantify the link with the
optional regression line and correlation coefficient. For ordered series
use LineChart.
Examples:
>>> from datachart.charts import ScatterChart
>>> # Basic scatter plot
>>> figure = ScatterChart(
... data=[
... {"x": 1, "y": 5},
... {"x": 2, "y": 10},
... {"x": 3, "y": 15},
... {"x": 4, "y": 20},
... {"x": 5, "y": 25}
... ],
... title="Basic Scatter Chart",
... xlabel="X",
... ylabel="Y"
... )
>>>
>>> # Scatter with hue grouping
>>> figure = ScatterChart(
... data=[
... {"x": 1, "y": 5, "category": "A"},
... {"x": 2, "y": 10, "category": "B"},
... ],
... hue="category",
... show_legend=True
... )
>>>
>>> # Bubble chart with size variable
>>> figure = ScatterChart(
... data=[
... {"x": 1, "y": 5, "pop": 100},
... {"x": 2, "y": 10, "pop": 200}
... ],
... size="pop",
... size_range=(20, 200)
... )
>>>
>>> # Scatter with regression line
>>> figure = ScatterChart(
... data=[...],
... show_regression=True,
... show_ci=True,
... ci_level=0.95
... )
>>>
>>> # Scatter with correlation annotation
>>> figure = ScatterChart(
... data=[...],
... show_correlation=True
... )
>>>
>>> # Scatter with a label beside each point
>>> figure = ScatterChart(
... data=[
... {"x": 1, "y": 5, "name": "A"},
... {"x": 2, "y": 10, "name": "B"}
... ],
... label="name"
... )
| PARAMETER | DESCRIPTION |
|---|---|
data
|
The data points for the scatter chart(s). Can be a single list of data points
for one chart, or a list of lists for multiple charts/subplots. A point
may carry its own
TYPE:
|
title
|
The title of the chart.
TYPE:
|
xlabel
|
The x-axis label.
TYPE:
|
ylabel
|
The y-axis label.
TYPE:
|
subtitle
|
The subtitle(s) for individual charts. Used as legend labels.
TYPE:
|
emphasis
|
The emphasis role(s) for individual charts, aligned like
TYPE:
|
emphasis_rule
|
A rule that highlights the series matching it and mutes the rest:
TYPE:
|
figsize
|
The size of the figure.
TYPE:
|
xmin
|
The minimum x-axis value.
TYPE:
|
xmax
|
The maximum x-axis value.
TYPE:
|
ymin
|
The minimum y-axis value.
TYPE:
|
ymax
|
The maximum y-axis value.
TYPE:
|
show_legend
|
Whether to show the legend.
TYPE:
|
legend
|
The per-figure legend setting: title, location, column count
and alignment; each field falls back to the theme. See
TYPE:
|
show_grid
|
Which grid lines to show (e.g., "both", "x", "y");
TYPE:
|
show_regression
|
Whether to show the regression line.
TYPE:
|
show_ci
|
Whether to show the confidence interval around the regression line.
TYPE:
|
ci_level
|
The confidence interval level (default 0.95).
TYPE:
|
show_correlation
|
Whether to show the Pearson correlation coefficient (r-value) as an annotation.
TYPE:
|
show_values
|
Whether to print each point's y value beside it. Cannot be
combined with
TYPE:
|
value_format
|
Format string for the value labels: a
TYPE:
|
value_step
|
Label every Nth point (
TYPE:
|
aspect_ratio
|
The aspect ratio of the axes ("auto" or "equal"). See
TYPE:
|
scalex
|
The x-axis scale (e.g., "log", "linear").
TYPE:
|
scaley
|
The y-axis scale (e.g., "log", "linear").
TYPE:
|
subplots
|
Whether to create separate subplots for each chart.
TYPE:
|
max_cols
|
Maximum number of columns in subplots (when subplots=True).
TYPE:
|
sharex
|
Whether to share the x-axis in subplots.
TYPE:
|
sharey
|
Whether to share the y-axis in subplots.
TYPE:
|
style
|
Style configuration(s) for the scatter markers.
TYPE:
|
xticks
|
Custom x-axis tick positions.
TYPE:
|
xticklabels
|
Custom x-axis tick labels.
TYPE:
|
xtickrotate
|
Rotation angle for x-axis tick labels.
TYPE:
|
yticks
|
Custom y-axis tick positions.
TYPE:
|
yticklabels
|
Custom y-axis tick labels.
TYPE:
|
ytickrotate
|
Rotation angle for y-axis tick labels.
TYPE:
|
xticks_format
|
The x-axis tick label format: a
TYPE:
|
yticks_format
|
The y-axis tick label format, as
TYPE:
|
vlines
|
Vertical line(s) to plot.
TYPE:
|
hlines
|
Horizontal line(s) to plot.
TYPE:
|
vspans
|
Vertical reference band(s) to shade, between two x positions.
TYPE:
|
hspans
|
Horizontal reference band(s) to shade, between two y positions.
TYPE:
|
texts
|
Text annotation(s) to draw.
TYPE:
|
x
|
The key name in data for x-axis values (default: "x").
TYPE:
|
y
|
The key name in data for y-axis values (default: "y").
TYPE:
|
size
|
The key name in data for marker size values (for bubble charts).
TYPE:
|
hue
|
The key name in data for color grouping (categorical variable).
TYPE:
|
label
|
The key name in data for the point labels (default: "label"),
aligned like
TYPE:
|
size_range
|
Tuple of (min_size, max_size) for bubble charts (default: (20, 200)).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
plt.Figure
|
The figure containing the scatter chart. |
Data
Each record in data is a ScatterDataPointAttrs; the x, y, size, hue and label parameters rename its keys.
datachart.typings.ScatterDataPointAttrs
Bases: TypedDict
The data point attributes for the scatter chart.
| ATTRIBUTE | DESCRIPTION |
|---|---|
x |
The x-axis value.
TYPE:
|
y |
The y-axis value.
TYPE:
|
size |
The marker size (for bubble charts).
TYPE:
|
hue |
The category for color grouping.
TYPE:
|
label |
The label drawn beside the point.
TYPE:
|
emphasis |
The point's own emphasis role
("background" or "highlight"); wins over the chart's
TYPE:
|
Style
style takes the keys of ScatterStyleAttrs. The chart also reads the shared groups it draws: value labels (ValueLabelStyleAttrs), the regression line (RegressionStyleAttrs), reference lines (VLineStyleAttrs and HLineStyleAttrs), reference bands (VSpanStyleAttrs and HSpanStyleAttrs) and text annotations (TextStyleAttrs). Every key falls back to the theme, so the same keys set the default look through config.
datachart.typings.ScatterStyleAttrs
Bases: TypedDict
The typing for the scatter chart style.
| ATTRIBUTE | DESCRIPTION |
|---|---|
plot_scatter_color |
The scatter marker color.
TYPE:
|
plot_scatter_alpha |
The alpha value of the markers.
TYPE:
|
plot_scatter_size |
The marker size.
TYPE:
|
plot_scatter_marker |
The marker shape.
TYPE:
|
plot_scatter_zorder |
The zorder of the scatter.
TYPE:
|
plot_scatter_edge_width |
The edge width of markers.
TYPE:
|
plot_scatter_edge_color |
The edge color of markers.
TYPE:
|
Constants
The parameters that accept a constant, with the class in datachart.constants that lists its values.
| Parameter | Constant |
|---|---|
emphasis |
EMPHASIS |
figsize |
FIG_SIZE |
legend={"location": ..., "alignment": ...} |
LEGEND_LOCATION, LEGEND_ALIGN |
show_grid |
SHOW_GRID |
value_format |
VALUE_FORMAT |
aspect_ratio |
ASPECT_RATIO |
scalex |
SCALE |
scaley |
SCALE |
xticks_format |
VALUE_FORMAT, DATE_FORMAT |
yticks_format |
VALUE_FORMAT, DATE_FORMAT |