Skip to main content

seaborn

Statistical visualization. Scatter, box, violin, heatmaps, pair plots, regression, correlation matrices, KDE, faceted plots, for exploratory analysis and publication figures.

설치로 이동

소스 정보

저장소
davila7/claude-code-templates
최근 소스 활동
2025년 12월 20일 21:13
감지된 SKILL.md 언어
영어
스타
30,896
포크
3,522

설치 방법

기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.

소스 파일 검토

설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.

파일 탐색기
4 개 파일

SKILL.md 표시 중

SKILL.md
소스 지침 · 읽기 전용 미리보기
name
seaborn
description
Statistical visualization. Scatter, box, violin, heatmaps, pair plots, regression, correlation matrices, KDE, faceted plots, for exploratory analysis and publication figures.
# Seaborn Statistical Visualization ## Overview Seaborn is a Python visualization library for creating publication-quality statistical graphics. Use this skill for dataset-oriented plotting, multivariate analysis, automatic statistical estimation, and complex multi-panel figures with minimal code. ## Design Philosophy Seaborn follows these core principles: 1. **Dataset-oriented**: Work directly with DataFrames and named variables rather than abstract coordinates 2. **Semantic mapping**: Automatically translate data values into visual properties (colors, sizes, styles) 3. **Statistical awareness**: Built-in aggregation, error estimation, and confidence intervals 4. **Aesthetic defaults**: Publication-ready themes and color palettes out of the box 5. **Matplotlib integration**: Full compatibility with matplotlib customization when needed ## Quick Start ```python import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # Load example dataset df = sns.load_dataset('tips') # Create a simple visualization sns.scatterplot(data=df, x='total_bill', y='tip', hue='day') plt.show() ``` ## Core Plotting Interfaces ### Function Interface (Traditional) The function interface provides specialized plotting functions organized by visualization type. Each category has **axes-level** functions (plot to single axes) and **figure-level** functions (manage entire figure with faceting). **When to use:** - Quick exploratory analysis - Single-purpose visualizations - When you need a specific plot type ### Objects Interface (Modern) The `seaborn.objects` interface provides a declarative, composable API similar to ggplot2. Build visualizations by chaining methods to specify data mappings, marks, transformations, and scales. **When to use:** - Complex layered visualizations - When you need fine-grained control over transformations - Building custom plot types - Programmatic plot generation ```python from seaborn import objects as so # Declarative syntax ( so.Plot(data=df, x='total_bill', y='tip') .add(so.Dot(), color='day') .add(so.Line(), so.PolyFit()) ) ``` ## Plotting Functions by Category ### Relational Plots (Relationships Between Variables) **Use for:** Exploring how two or more variables relate to each other - `scatterplot()` - Display individual observations as points - `lineplot()` - Show trends and changes (automatically aggregates and computes CI) - `relplot()` - Figure-level interface with automatic faceting **Key parameters:** - `x`, `y` - Primary variables - `hue` - Color encoding for additional categorical/continuous variable - `size` - Point/line size encoding - `style` - Marker/line style encoding - `col`, `row` - Facet into multiple subplots (figure-level only) ```python # Scatter with multiple semantic mappings sns.scatterplot(data=df, x='total_bill', y='tip', hue='time', size='size', style='sex') # Line plot with confidence intervals sns.lineplot(data=timeseries, x='date', y='value', hue='category') # Faceted relational plot sns.relplot(data=df, x='total_bill', y='tip', col='time', row='sex', hue='smoker', kind='scatter') ``` ### Distribution Plots (Single and Bivariate Distributions) **Use for:** Understanding data spread, shape, and probability density - `histplot()` - Bar-based frequency distributions with flexible binning - `kdeplot()` - Smooth density estimates using Gaussian kernels - `ecdfplot()` - Empirical cumulative distribution (no parameters to tune) - `rugplot()` - Individual observation tick marks - `displot()` - Figure-level interface for univariate and bivariate distributions - `jointplot()` - Bivariate plot with marginal distributions - `pairplot()` - Matrix of pairwise relationships across dataset **Key parameters:** - `x`, `y` - Variables (y optional for univariate) - `hue` - Separate distributions by category - `stat` - Normalization: "count", "frequency", "probability", "density" - `bins` / `binwidth` - Histogram binning control - `bw_adjust` - KDE bandwidth multiplier (higher = smoother) - `fill` - Fill area under curve - `multiple` - How to handle hue: "layer", "stack", "dodge", "fill" ```python # Histogram with density normalization sns.histplot(data=df, x='total_bill', hue='time', stat='density', multiple='stack') # Bivariate KDE with contours sns.kdeplot(data=df, x='total_bill', y='tip', fill=True, levels=5, thresh=0.1) # Joint plot with marginals sns.jointplot(data=df, x='total_bill', y='tip', kind='scatter', hue='time') # Pairwise relationships sns.pairplot(data=df, hue='species', corner=True) ``` ### Categorical Plots (Comparisons Across Categories) **Use for:** Comparing distributions or statistics across discrete categories **Categorical scatterplots:** - `stripplot()` - Points with jitter to show all observations - `swarmplot()` - Non-overlapping points (beeswarm algorithm) **Distribution comparisons:** - `boxplot()` - Quartiles and outliers - `violinplot()` - KDE + quartile information - `boxenplot()` - Enhanced boxplot for larger datasets **Statistical estimates:** - `barplot()` - Mean/aggregate with confidence intervals - `pointplot()` - Point estimates with connecting lines - `countplot()` - Count of observations per category **Figure-level:** - `catplot()` - Faceted categorical plots (set `kind` parameter) **Key parameters:** - `x`, `y` - Variables (one typically categorical) - `hue` - Additional categorical grouping - `order`, `hue_order` - Control category ordering - `dodge` - Separate hue levels side-by-side - `orient` - "v" (vertical) or "h" (horizontal) - `kind` - Plot type for catplot: "strip", "swarm", "box", "violin", "bar", "point" ```python # Swarm plot showing all points sns.swarmplot(data=df, x='day', y='total_bill', hue='sex') # Violin plot with split for comparison sns.violinplot(data=df, x='day', y='total_bill', hue='sex', split=True) # Bar plot with error bars sns.barplot(data=df, x='day', y='total_bill', hue='sex', estimator='mean', errorbar='ci') # Faceted categorical plot sns.catplot(data=df, x='day', y='total_bill', col='time', kind='box') ``` ### Regression Plots (Linear Relationships) **Use for:** Visualizing linear regressions and residuals - `regplot()` - Axes-level regression plot with scatter + fit line - `lmplot()` - Figure-level with faceting support - `residplot()` - Residual plot for assessing model fit **Key parameters:** - `x`, `y` - Variables to regress - `order` - Polynomial regression order - `logistic` - Fit logistic regression - `robust` - Use robust regression (less sensitive to outliers) - `ci` - Confidence interval width (default 95) - `scatter_kws`, `line_kws` - Customize scatter and line properties ```python # Simple linear regression sns.regplot(data=df, x='total_bill', y='tip') # Polynomial regression with faceting sns.lmplot(data=df, x='total_bill', y='tip', col='time', order=2, ci=95) # Check residuals sns.residplot(data=df, x='total_bill', y='tip') ``` ### Matrix Plots (Rectangular Data) **Use for:** Visualizing matrices, correlations, and grid-structured data - `heatmap()` - Color-encoded matrix with annotations - `clustermap()` - Hierarchically-clustered heatmap **Key parameters:** - `data` - 2D rectangular dataset (DataFrame or array) - `annot` - Display values in cells - `fmt` - Format string for annotations (e.g., ".2f") - `cmap` - Colormap name - `center` - Value at colormap center (for diverging colormaps) - `vmin`, `vmax` - Color scale limits - `square` - Force square cells - `linewidths` - Gap between cells ```python # Correlation heatmap corr = df.corr() sns.heatmap(corr, annot=True, fmt='.2f', cmap='coolwarm', center=0, square=True) # Clustered heatmap sns.clustermap(data, cmap='viridis', standard_scale=1, figsize=(10, 10)) ``` ## Multi-Plot Grids Seaborn provides grid objects for creating complex multi-panel figures: ### FacetGrid Create subplots based on categorical variables. Most useful when called through figure-level functions (`relplot`, `displot`, `catplot`), but can be used directly for custom plots. ```python g = sns.FacetGrid(df, col='time', row='sex', hue='smoker') g.map(sns.scatterplot, 'total_bill', 'tip') g.add_legend() ``` ### PairGrid Show pairwise relationships between all variables in a dataset. ```python g = sns.PairGrid(df, hue='species') g.map_upper(sns.scatterplot) g.map_lower(sns.kdeplot) g.map_diag(sns.histplot) g.add_legend() ``` ### JointGrid Combine bivariate plot with marginal distributions. ```python g = sns.JointGrid(data=df, x='total_bill', y='tip') g.plot_joint(sns.scatterplot) g.plot_marginals(sns.histplot) ``` ## Figure-Level vs Axes-Level Functions Understanding this distinction is crucial for effective seaborn usage: ### Axes-Level Functions - Plot to a single matplotlib `Axes` object - Integrate easily into complex matplotlib figures - Accept `ax=` parameter for precise placement - Return `Axes` object - Examples: `scatterplot`, `histplot`, `boxplot`, `regplot`, `heatmap` **When to use:** - Building custom multi-plot layouts - Combining different plot types - Need matplotlib-level control - Integrating with existing matplotlib code ```python fig, axes = plt.subplots(2, 2, figsize=(10, 10)) sns.scatterplot(data=df, x='x', y='y', ax=axes[0, 0]) sns.histplot(data=df, x='x', ax=axes[0, 1]) sns.boxplot(data=df, x='cat', y='y', ax=axes[1, 0]) sns.kdeplot(data=df, x='x', y='y', ax=axes[1, 1]) ``` ### Figure-Level Functions - Manage entire figure including all subplots - Built-in faceting via `col` and `row` parameters - Return `FacetGrid`, `JointGrid`, or `PairGrid` objects - Use `height` and `aspect` for sizing (per subplot) - Cannot be placed in existing figure - Examples: `relplot`, `displot`, `catplot`, `lmplot`, `jointplot`, `pairplot` **When to use:** - Faceted visualizations (small multiples) - Quick exploratory analysis - Consistent multi-panel layouts - Don't need to combine with other plot types ```python # Automatic faceting sns.relplot(data=df, x='x', y='y', col='category', row='group', hue='type', height=3, aspect=1.2) ``` ## Data Structure Requirements ### Long-Form Data (Preferred) Each variable is a column, each observation is a row. This "tidy" format provides maximum flexibility: ```python # Long-form structure subject condition measurement 0 1 control 10.5 1 1 treatment 12.3 2 2 control 9.8 3 2 treatment 13.1 ``` **Advantages:** - Works with all seaborn functions - Easy to remap variables to visual properties - Supports arbitrary complexity - Natural for DataFrame operations ### Wide-Form Data Variables are spread across columns. Useful for simple rectangular data: ```python # Wide-form structure control treatment 0 10.5 12.3 1 9.8 13.1 ``` **Use cases:** - Simple time series - Correlation matrices - Heatmaps - Quick plots of array data **Converting wide to long:** ```python df_long = df.melt(var_name='condition', value_name='measurement') ``` ## Color Palettes Seaborn provides carefully designed color palettes for different data types: ### Qualitative Palettes (Categorical Data) Distinguish categories through hue variation: - `"deep"` - Default, vivid colors - `"muted"` - Softer, less saturated - `"pastel"` - Light, desaturated - `"bright"` - Highly saturated - `"dark"` - Dark values - `"colorblind"` - Safe for color vision deficiency ```python sns.set_palette("colorblind") sns.color_palette("Set2") ``` ### Sequential Palettes (Ordered Data) Show progression from low to high values: - `"rocket"`, `"mako"` - Wide luminance range (good for heatmaps) - `"flare"`, `"crest"` - Restricted luminance (good for points/lines) - `"viridis"`, `"magma"`, `"plasma"` - Matplotlib perceptually uniform ```python
GitHub에서 보기
이 SKILL.md는 매우 커서 SkillsMP가 여기에는 첫 섹션만 미리 보여줍니다. GitHub에서 보기