_
_
Seaborn Cheatsheet¶
• Installation
| Platform | Command |
|---|---|
| Ubuntu/Debian | INLINE_CODE_10 or INLINE_CODE_11 |
| macOS | INLINE_CODE_12 or INLINE_CODE_13 |
| Windows | INLINE_CODE_14 or INLINE_CODE_15 |
| Conda (all platforms) | INLINE_CODE_16 |
| Specific version | INLINE_CODE_17 |
| With dependencies | INLINE_CODE_18 |
| Upgrade existing | INLINE_CODE_19 |
| Verify installation | INLINE_CODE_20 |
| _ | |
| oder Grundlegende Befehle |
| 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.) |
| _ | |
| / Fortgeschrittene Nutzung |
| 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 |
| _ | |
| Konfiguration |
Theme and Style Configuration¶
# 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}
)
Benutzerdefinierte Farbpaletten¶
# 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)
Temporärer Stil Kontext¶
# Apply style temporarily
with sns.axes_style("white"):
sns.boxplot(data=df, x='category', y='value')
plt.show()
# Original style restored after block
Variable Funktionsparameter¶
# 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
)
Häufige Anwendungsfälle
Use Case 1: Exploratory Datenanalyse¶
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 Vergleich¶
# 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 Visualisierung¶
# 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 Facettenanalyse¶
# 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: Statistische Regressionsanalyse¶
# 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()
oder Best Practices
-
** Verwenden Sie Figuren-Level-Funktionen für Facetten**: Funktionen wie
relplot(), __INLINE_CODE_62_ undlmplot()automatisch mit der Erstellung von Figuren und Achsen umgehen, so dass gesichtete Handlungen einfacher zu verwalten -
**Wählen Sie entsprechende Grundstückstypen*: Verwenden Sie Geigendiagramme zum Verteilungsvergleich, Felddiagramme zur Ausreißerdetektion, Swarm-Plots für kleine Datensätze und Streifen-Plots für mittlere Datensätze
-
Verwalten Sie den
dataParameter: Verwenden Sie immer dendataParameter mit pandas DataFrames für saubereren Code und bessere Integration mit Daten Manipulation Workflows -
**Die Themen früh einrichten*: Rufen Sie
sns.set_theme()odersns.set_style()_ am Anfang Ihres Skripts an, um ein einheitliches Styling über alle Plots sicherzustellen -
** Verwenden Sie semantische Mappings*: Nutzen Sie die
hue, __INLINE_CODE_69_ undstyleParameter, um zusätzliche Dimensionen in Ihrer Visualisierung zu kodieren, ohne separate Plots zu erstellen -
**Save hochauflösende Zahlen*: Verwenden
plt.savefig('file.png', dpi=300, bbox_inches='tight')für Veröffentlichungsqualitätsbilder mit richtigen Margen -
** Kombinieren Sie Seaborn mit Matplotlib**: Verwenden Sie Seaborn für schnelle statistische Plots und Matplotlib für fein abgestimmte Anpassung (Titel, Labels, Anmerkungen, etc.)
-
**Große Datensätze effizient handhaben*: Bei Datensätzen mit > 10.000 Punkten ist die Verwendung
rasterized=Truein Streufeldern oder die Aggregation von Daten vor der Erstellung zu berücksichtigen. -
** Verwenden Sie Farbpaletten strategisch*: Wählen Sie sequentielle Paletten für bestellte Daten, divergierende Paletten für Daten mit einem aussagekräftigen Zentrum und qualitativen Paletten für kategorische Daten
-
**Dokumentieren Sie Ihre Farbwahl*: Bei der Erstellung von Visualisierungen für colorblind-Publikum verwenden Sie colorblind-freundliche Paletten wie __INLINE_CODE_73_ oder testen Sie mit
sns.color_palette("colorblind")_
Fehlerbehebung
| 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 |
| Importfehler nach der Installation | Starten Sie Python kernel/IDE aus oder überprüfen Sie die Installation mit pip show seaborn und überprüfen Sie Abhängigkeiten |