prepare_scatter_plot

prepare_scatter_plot(df, x, y, *, color=None, size=None, loess=0, alpha=None)

Scatter plot of y against x with optional aesthetics and a LOESS smoother.

Parameters

Name Type Description Default
df pd.DataFrame Data frame containing the variables. required
x str Column names for the axes. required
y str Column names for the axes. required
color str | None Optional column mapped to marker color (numeric -> colorbar, otherwise discrete). None
size str | None Optional numeric column mapped to marker size. None
loess Literal[0, 1, 2] 0 no smoother, 1 unweighted LOESS, 2 LOESS weighted by size. 0
alpha float | None Marker opacity. If None, a sample-size-based default is used. None

Returns

Name Type Description
plotly.graph_objects.Figure The scatter figure.

Examples

Basic — a plain scatter of two variables (this function returns a Plotly figure directly, so there is no .fig attribute):

import expdpy as ex
from expdpy.data import load_kuznets

df = load_kuznets()
ex.prepare_scatter_plot(df, x="log_gdp_pc", y="gini_regional")

Advanced — map color and marker size to other columns, add a size-weighted LOESS smoother (the N-shaped Kuznets curve) and tune opacity:

ex.prepare_scatter_plot(
    df, x="log_gdp_pc", y="gini_regional",
    color="continent", size="population", loess=2, alpha=0.6,
)