Saltar a contenido

Seaborn Cheatsheet

Instalación

__TABLE_114_

Comandos básicos

Command Description
INLINE_CODE_21 Standard import convention
INLINE_CODE_22 Apply default Seaborn styling
INLINE_CODE_23 Set plot style (whitegrid, darkgrid, white, dark, ticks)
INLINE_CODE_24 Set scaling context (paper, notebook, talk, poster)
INLINE_CODE_25 Create histogram with kernel density estimate
INLINE_CODE_26 Plot kernel density estimate
INLINE_CODE_27 Create box plot for categorical data
INLINE_CODE_28 Create violin plot (box + KDE)
INLINE_CODE_29 Create scatter plot with color grouping
INLINE_CODE_30 Create line plot with confidence intervals
INLINE_CODE_31 Create bar plot with error bars
INLINE_CODE_32 Count occurrences of categorical values
INLINE_CODE_33 Create annotated correlation heatmap
INLINE_CODE_34 Create scatter plot matrix for all variables
INLINE_CODE_35 Scatter plot with linear regression line
INLINE_CODE_36 Categorical scatter plot (all points visible)
INLINE_CODE_37 Non-overlapping categorical scatter plot
INLINE_CODE_38 Scatter plot with marginal distributions
INLINE_CODE_39 Get current color palette
INLINE_CODE_40 Set color palette (husl, Set1, Set2, deep, etc.)

Advanced Usage

Command Description
INLINE_CODE_41 Regression plot with faceting by columns
INLINE_CODE_42 Categorical plot with faceting
INLINE_CODE_43 Relational plot with row/column faceting
INLINE_CODE_44 Hierarchical clustered heatmap
INLINE_CODE_45 2D bivariate KDE plot
INLINE_CODE_46 Empirical cumulative distribution function
INLINE_CODE_47 Stacked histogram for multiple categories
INLINE_CODE_48 Split violin plot for comparison
INLINE_CODE_49 Polynomial regression (order 3)
INLINE_CODE_50 Robust regression (outlier-resistant)
INLINE_CODE_51 Plot residuals of regression
INLINE_CODE_52 Point plot with error bars
INLINE_CODE_53 Create diverging color palette
INLINE_CODE_54 Create cubehelix color palette
INLINE_CODE_55 Remove plot spines for cleaner look
INLINE_CODE_56 Add rug plot (marginal tick marks)
INLINE_CODE_57 Custom faceted plot grid
INLINE_CODE_58 Horizontal bar plot with 95% CI
INLINE_CODE_59 Heatmap with centered diverging colors
INLINE_CODE_60 Reposition legend outside plot

Configuración

Tema y configuración de estilo

# Comprehensive theme setup
sns.set_theme(
    style="darkgrid",           # whitegrid, darkgrid, white, dark, ticks
    palette="deep",             # Color palette
    font="sans-serif",          # Font family
    font_scale=1.2,             # Scale all fonts
    color_codes=True,           # Use color codes
    rc={                        # Matplotlib rcParams
        "figure.figsize": (12, 8),
        "axes.labelsize": 14,
        "axes.titlesize": 16,
        "xtick.labelsize": 12,
        "ytick.labelsize": 12,
        "legend.fontsize": 12
    }
)

Context-Based Scaling

# Scale plots for different contexts
sns.set_context(
    "talk",                     # paper, notebook, talk, poster
    font_scale=1.5,
    rc={"lines.linewidth": 2.5}
)

Paletas de color personalizado

# Define custom color palette
custom_colors = ["#FF6B6B", "#4ECDC4", "#45B7D1", "#FFA07A", "#98D8C8"]
sns.set_palette(sns.color_palette(custom_colors))

# Or use built-in palettes
sns.set_palette("Set2", n_colors=8)

Temporary Style Context

# Apply style temporarily
with sns.axes_style("white"):
    sns.boxplot(data=df, x='category', y='value')
    plt.show()
# Original style restored after block

Diámetros de función de la Figura-Nivel

# Common parameters for figure-level functions
g = sns.relplot(
    data=df,
    x='x', y='y',
    hue='category',
    col='group',
    col_wrap=3,              # Wrap columns after 3
    height=4,                # Height of each facet
    aspect=1.5,              # Aspect ratio (width = height * aspect)
    palette='viridis',
    legend='full'            # 'auto', 'brief', 'full', False
)

Common Use Cases

Use Case 1: Exploratory Análisis de datos

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

# Load data
df = pd.read_csv('data.csv')

# Quick overview with pairplot
sns.pairplot(df, hue='target_variable', diag_kind='kde')
plt.savefig('pairplot.png', dpi=300, bbox_inches='tight')

# Correlation heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(df.corr(), annot=True, fmt='.2f', cmap='coolwarm', center=0)
plt.title('Feature Correlation Matrix')
plt.tight_layout()
plt.savefig('correlation.png', dpi=300)

Use Case 2: Distribution Comparison

# Compare distributions across categories
fig, axes = plt.subplots(2, 2, figsize=(14, 10))

# Histogram with KDE
sns.histplot(data=df, x='value', hue='category', kde=True, ax=axes[0, 0])
axes[0, 0].set_title('Distribution with KDE')

# Violin plot
sns.violinplot(data=df, x='category', y='value', ax=axes[0, 1])
axes[0, 1].set_title('Violin Plot')

# Box plot with swarm overlay
sns.boxplot(data=df, x='category', y='value', ax=axes[1, 0])
sns.swarmplot(data=df, x='category', y='value', color='black', alpha=0.3, ax=axes[1, 0])
axes[1, 0].set_title('Box + Swarm Plot')

# ECDF
sns.ecdfplot(data=df, x='value', hue='category', ax=axes[1, 1])
axes[1, 1].set_title('Cumulative Distribution')

plt.tight_layout()
plt.show()

Use Case 3: Time Series Visualization

# Time series with confidence intervals
df['date'] = pd.to_datetime(df['date'])

# Line plot with shaded confidence interval
plt.figure(figsize=(14, 6))
sns.lineplot(
    data=df,
    x='date',
    y='value',
    hue='category',
    style='category',
    markers=True,
    dashes=False,
    ci=95,
    err_style='band'
)
plt.xticks(rotation=45)
plt.title('Time Series with 95% Confidence Intervals')
plt.tight_layout()
plt.show()

Use Case 4: Multi-Variable Faceted Analysis

# Create faceted plot for complex relationships
g = sns.FacetGrid(
    df,
    col='region',
    row='product_type',
    hue='customer_segment',
    height=4,
    aspect=1.2,
    margin_titles=True
)

g.map_dataframe(sns.scatterplot, x='price', y='sales', alpha=0.6)
g.add_legend(title='Customer Segment')
g.set_axis_labels('Price ($)', 'Sales Volume')
g.set_titles(col_template="{col_name}", row_template="{row_name}")
g.tight_layout()
plt.savefig('faceted_analysis.png', dpi=300, bbox_inches='tight')

Use Case 5: Statistical Regression Analysis

# Multiple regression visualizations
fig, axes = plt.subplots(1, 3, figsize=(18, 5))

# Linear regression
sns.regplot(data=df, x='feature1', y='target', ax=axes[0])
axes[0].set_title('Linear Regression')

# Polynomial regression
sns.regplot(data=df, x='feature2', y='target', order=3, ax=axes[1])
axes[1].set_title('Polynomial Regression (Order 3)')

# Residual plot
sns.residplot(data=df, x='feature1', y='target', ax=axes[2])
axes[2].set_title('Residual Plot')
axes[2].axhline(0, ls='--', color='red', alpha=0.5)

plt.tight_layout()
plt.show()

Buenas prácticas

  • Usar funciones de nivel de figuras para enfrentar: Funciones como relplot(), catplot() y __INLINE_CODE_63 manejan automáticamente la figura y los ejes de creación, facilitando la gestión de las tramas facetadas

  • Elija tipos de trama adecuados: Use tramas de violín para la comparación de distribución, tramas de caja para la detección de alicates, parcelas enjambre para pequeños conjuntos de datos y tramas de tira para conjuntos de datos de tamaño mediano

Aprobar el parámetro data: Utilizar siempre el parámetro data con pandas DataFrames for cleaner code and better integration with data manipulation workflows

  • Temas iniciales: Llame sns.set_theme() o sns.set_style() al comienzo de su script para asegurar un estilo consistente en todas las parcelas

  • Utilice cartografías semánticas: Aproveche los parámetros hue, size y style para codificar dimensiones adicionales en sus visualizaciones sin crear parcelas separadas

Guardar cifras de alta resolución: Uso plt.savefig('file.png', dpi=300, bbox_inches='tight') para imágenes de calidad de publicación con márgenes adecuados

  • El mar marino con Matplotlib**: Use Seaborn para parcelas estadísticas rápidas y Matplotlib para la personalización fina (títulos, etiquetas, anotaciones, etc.)

  • Mantener grandes conjuntos de datos de manera eficiente: Para conjuntos de datos con √10,000 puntos, considere el uso rasterized=True_ en parcelas de dispersión o datos de agregación antes de trazar

  • Use paletas de colores estratégicamente: Elija paletas secuenciales para datos ordenados, paletas divergentes para datos con un centro significativo, y paletas cualitativas para datos categóricos

  • Documente sus opciones de color: Al crear visualizaciones para públicos colorblind, utilice paletas colorblind-friendly como __INLINE_CODE_73_ o prueba con sns.color_palette("colorblind")

Troubleshooting

Issue Solution
Plot not displaying Add INLINE_CODE_75 at the end or use INLINE_CODE_76 in Jupyter notebooks
Legend overlapping plot Use INLINE_CODE_77 or INLINE_CODE_78
Categorical axis labels overlapping Rotate labels with INLINE_CODE_79 or use horizontal plots with INLINE_CODE_80
Heatmap annotations too small Increase figure size INLINE_CODE_81 or adjust font size INLINE_CODE_82
Color palette not applying Set palette before creating plot with INLINE_CODE_83 or use INLINE_CODE_84 parameter directly in plot function
FacetGrid plots too small Increase INLINE_CODE_85 parameter (e.g., INLINE_CODE_86) and adjust INLINE_CODE_87 ratio (e.g., INLINE_CODE_88)
Missing confidence intervals Ensure sufficient data points; CI requires multiple observations per x-value. Use INLINE_CODE_89 to disable
Seaborn overriding Matplotlib settings Reset with INLINE_CODE_90 or INLINE_CODE_91 to restore Matplotlib defaults
Memory issues with large datasets Use INLINE_CODE_92 for scatter plots or downsample data before plotting
KDE plot looks wrong Adjust INLINE_CODE_93 parameter (e.g., INLINE_CODE_94 for narrower bandwidth) or use INLINE_CODE_95 to limit range
Regression line not fitting data Try polynomial regression with INLINE_CODE_96 or INLINE_CODE_97, or use INLINE_CODE_98 for outlier resistance
Heatmap colors not centered Use INLINE_CODE_99 parameter and diverging colormap like INLINE_CODE_100 for data with meaningful zero point
Plot style not persisting Use INLINE_CODE_101 instead of INLINE_CODE_102 (deprecated) or set INLINE_CODE_103 parameters explicitly
Facet titles cut off Add INLINE_CODE_104 or use INLINE_CODE_105 for FacetGrid objects
eterna ** Errores de importación después de la instalación** Ø Restart Python kernel/IDE o verifique la instalación con pip show seaborn_ y verifique las dependencias