Statistical data visualization library for Python built on matplotlib
—
Build complex multi-panel figures with FacetGrid, PairGrid, and JointGrid for comprehensive data exploration, pairwise relationship analysis, and bivariate plots with marginal distributions. These classes provide flexible frameworks for creating sophisticated statistical visualizations.
Multi-plot grid for plotting conditional relationships across subsets of data.
class FacetGrid:
def __init__(
self,
data,
*,
row=None,
col=None,
hue=None,
col_wrap=None,
sharex=True,
sharey=True,
height=3,
aspect=1,
palette=None,
row_order=None,
col_order=None,
hue_order=None,
hue_kws=None,
dropna=False,
legend_out=True,
despine=True,
margin_titles=False,
xlim=None,
ylim=None,
subplot_kws=None,
gridspec_kws=None,
**kwargs
):
"""
Initialize the matplotlib figure and FacetGrid object.
Parameters:
- data: DataFrame, tidy dataset for plotting
- row, col: str, variables for faceting into subplots
- hue: str, grouping variable for color mapping
- col_wrap: int, wrap columns after this number
- sharex, sharey: bool, share axes across subplots
- height: float, height of each facet in inches
- aspect: float, aspect ratio of each facet
- palette: str or list, colors for hue levels
- row_order, col_order, hue_order: list, order for categorical levels
- dropna: bool, drop missing values before plotting
- legend_out: bool, draw legend outside the figure
- margin_titles: bool, draw titles on the margins
"""
def map(self, func, *args, **kwargs):
"""
Apply a plotting function to each facet's subset of data.
Parameters:
- func: callable, plotting function
- args: positional arguments for func
- kwargs: keyword arguments for func
"""
def map_dataframe(self, func, *args, **kwargs):
"""
Like map but passes the DataFrame to the function.
"""
def add_legend(self, legend_data=None, title=None, label_order=None, **kwargs):
"""Add a legend to the figure."""
def savefig(self, *args, **kwargs):
"""Save the figure."""Subplot grid for plotting pairwise relationships in a dataset.
class PairGrid:
def __init__(
self,
data,
*,
hue=None,
vars=None,
x_vars=None,
y_vars=None,
hue_order=None,
palette=None,
hue_kws=None,
corner=False,
diag_sharey=True,
height=2.5,
aspect=1,
layout_pad=0.5,
despine=True,
dropna=False,
**kwargs
):
"""
Initialize the plot figure and PairGrid object.
Parameters:
- data: DataFrame, tidy dataset for plotting
- hue: str, grouping variable for color mapping
- vars: list, variables to include in grid
- x_vars, y_vars: list, separate variables for x and y axes
- corner: bool, plot only lower triangle
- diag_sharey: bool, share y-axis on diagonal plots
- height: float, height of each facet in inches
- aspect: float, aspect ratio of each facet
"""
def map(self, func, **kwargs):
"""Plot with the same function in every subplot."""
def map_diag(self, func, **kwargs):
"""Plot with a univariate function on the diagonal subplots."""
def map_upper(self, func, **kwargs):
"""Plot with a bivariate function on the upper triangle."""
def map_lower(self, func, **kwargs):
"""Plot with a bivariate function on the lower triangle."""
def map_offdiag(self, func, **kwargs):
"""Plot with a bivariate function on the off-diagonal subplots."""
def add_legend(self, legend_data=None, title=None, label_order=None, **kwargs):
"""Add a legend to the figure."""Grid for drawing bivariate plots with marginal univariate plots.
class JointGrid:
def __init__(
self,
data=None,
*,
x=None,
y=None,
hue=None,
height=6,
ratio=5,
space=0.2,
dropna=False,
xlim=None,
ylim=None,
marginal_ticks=False,
hue_order=None,
palette=None,
hue_kws=None,
**kwargs
):
"""
Set up the grid of subplots and store data internally.
Parameters:
- data: DataFrame, tidy dataset
- x, y: str, variable names in data
- hue: str, grouping variable for color mapping
- height: float, size of the figure (square)
- ratio: float, ratio of joint axes height to marginal axes height
- space: float, space between joint and marginal axes
- marginal_ticks: bool, show ticks on marginal axes
"""
def plot(self, joint_func, marginal_func, **kwargs):
"""Plot joint and marginal distributions."""
def plot_joint(self, func, **kwargs):
"""Draw a bivariate plot on the joint axes."""
def plot_marginals(self, func, **kwargs):
"""Draw univariate plots on the marginal axes."""
def refline(self, *, x=None, y=None, joint=True, marginal=True, **line_kws):
"""Add reference lines to the plot."""Plot pairwise relationships in a dataset using a PairGrid.
def pairplot(
data,
*,
hue=None,
hue_order=None,
palette=None,
vars=None,
x_vars=None,
y_vars=None,
kind="scatter",
diag_kind="auto",
markers=None,
height=2.5,
aspect=1,
corner=False,
dropna=False,
plot_kws=None,
diag_kws=None,
grid_kws=None,
**kwargs
):
"""
Plot pairwise relationships in a dataset.
Parameters:
- data: DataFrame, tidy dataset for plotting
- hue: str, grouping variable for color mapping
- vars: list, variables to include
- kind: str, plot type for off-diagonal ("scatter", "kde", "hist", "reg")
- diag_kind: str, plot type for diagonal ("auto", "hist", "kde", None)
- markers: str or list, marker styles for hue levels
- height: float, height of each facet
- corner: bool, plot only lower triangle
- plot_kws: dict, keyword arguments for off-diagonal plots
- diag_kws: dict, keyword arguments for diagonal plots
- grid_kws: dict, keyword arguments for PairGrid
Returns:
PairGrid object
"""Draw bivariate plots with marginal univariate plots.
def jointplot(
data=None,
*,
x=None,
y=None,
hue=None,
kind="scatter",
height=6,
ratio=5,
space=0.2,
dropna=False,
xlim=None,
ylim=None,
color=None,
palette=None,
hue_order=None,
hue_norm=None,
marginal_ticks=False,
joint_kws=None,
marginal_kws=None,
**kwargs
):
"""
Draw a plot of two variables with bivariate and univariate graphs.
Parameters:
- data: DataFrame, tidy dataset
- x, y: str, variable names in data
- hue: str, grouping variable for color mapping
- kind: str, plot type ("scatter", "kde", "hist", "hex", "reg", "resid")
- height: float, size of the figure (square)
- ratio: float, ratio of joint to marginal axes
- space: float, space between joint and marginal axes
- joint_kws: dict, keyword arguments for joint plot
- marginal_kws: dict, keyword arguments for marginal plots
Returns:
JointGrid object
"""import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
# Create FacetGrid and map a plotting function
g = sns.FacetGrid(tips, col="time", row="smoker", height=4)
g.map(sns.scatterplot, "total_bill", "tip", alpha=0.7)
g.add_legend()
plt.show()# Map different functions to the same grid
g = sns.FacetGrid(tips, col="day", hue="smoker", height=4)
g.map(sns.scatterplot, "total_bill", "tip", alpha=0.7)
g.map(sns.regplot, "total_bill", "tip", scatter=False)
g.add_legend()
plt.show()iris = sns.load_dataset("iris")
# Create PairGrid and map different functions
g = sns.PairGrid(iris, hue="species")
g.map_diag(sns.histplot)
g.map_upper(sns.scatterplot)
g.map_lower(sns.kdeplot)
g.add_legend()
plt.show()# Quick pairwise relationships
sns.pairplot(iris, hue="species", diag_kind="kde")
plt.show()# Create JointGrid and customize plots
g = sns.JointGrid(data=tips, x="total_bill", y="tip")
g.plot_joint(sns.scatterplot, alpha=0.7)
g.plot_marginals(sns.histplot, kde=True)
plt.show()# Quick bivariate plot with marginals
sns.jointplot(
data=tips, x="total_bill", y="tip",
kind="reg", height=8
)
plt.show()# Show only lower triangle
sns.pairplot(iris, hue="species", corner=True)
plt.show()# Different combinations of joint and marginal plots
sns.jointplot(
data=tips, x="total_bill", y="tip",
kind="hex", # Hexbin for joint
marginal_kws=dict(bins=25) # Custom marginal bins
)
plt.show()# Grid classes
FacetGrid = seaborn.axisgrid.FacetGrid
PairGrid = seaborn.axisgrid.PairGrid
JointGrid = seaborn.axisgrid.JointGrid
# Plot kinds for pairplot
PairPlotKind = Literal["scatter", "kde", "hist", "reg"]
DiagKind = Literal["auto", "hist", "kde", None]
# Plot kinds for jointplot
JointPlotKind = Literal["scatter", "kde", "hist", "hex", "reg", "resid"]Install with Tessl CLI
npx tessl i tessl/pypi-seaborn