4 min read min read
Seaborn Themes and Styles
Learn to customize Seaborn charts with themes
Seaborn Themes and Styles
Built-in Themes
Seaborn has 5 built-in themes:
code.py
import seaborn as sns
import matplotlib.pyplot as plt
# Set theme (choose one)
sns.set_theme(style='darkgrid') # Gray background with grid
sns.set_theme(style='whitegrid') # White background with grid
sns.set_theme(style='dark') # Gray background, no grid
sns.set_theme(style='white') # White background, no grid
sns.set_theme(style='ticks') # White with tick marksCompare Themes
code.py
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({
'x': range(10),
'y': [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
})
themes = ['darkgrid', 'whitegrid', 'dark', 'white', 'ticks']
fig, axes = plt.subplots(1, 5, figsize=(20, 4))
for ax, theme in zip(axes, themes):
sns.set_theme(style=theme)
sns.lineplot(data=df, x='x', y='y', ax=ax)
ax.set_title(theme)
plt.tight_layout()
plt.show()Color Palettes
code.py
# Set color palette
sns.set_palette('pastel')
sns.set_palette('husl')
sns.set_palette('Set2')
sns.set_palette('deep')Use Specific Palette
code.py
sns.barplot(data=df, x='x', y='y', palette='Blues')
sns.barplot(data=df, x='x', y='y', palette='Greens')
sns.barplot(data=df, x='x', y='y', palette='rocket')View Available Palettes
code.py
# See color palette
sns.color_palette('Set2')
sns.palplot(sns.color_palette('Set2'))
plt.show()Set Figure Size
code.py
# Method 1: Use matplotlib
plt.figure(figsize=(10, 6))
sns.barplot(data=df, x='x', y='y')
# Method 2: Use sns.set_theme
sns.set_theme(rc={'figure.figsize': (10, 6)})Customize Font Size
code.py
sns.set_theme(font_scale=1.5) # 1.5x larger fontsComplete Theme Setup
code.py
sns.set_theme(
style='whitegrid',
palette='Set2',
font_scale=1.2,
rc={'figure.figsize': (10, 6)}
)Reset to Default
code.py
sns.reset_defaults()Professional Chart Example
code.py
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# Set professional theme
sns.set_theme(
style='whitegrid',
palette='deep',
font_scale=1.1
)
# Sample data
df = pd.DataFrame({
'Quarter': ['Q1', 'Q2', 'Q3', 'Q4'],
'Revenue': [100, 120, 115, 140],
'Profit': [20, 25, 22, 35]
})
# Create chart
plt.figure(figsize=(10, 6))
ax = sns.barplot(data=df, x='Quarter', y='Revenue', color='steelblue')
# Add title and labels
ax.set_title('Quarterly Revenue', fontsize=16, fontweight='bold')
ax.set_xlabel('Quarter', fontsize=12)
ax.set_ylabel('Revenue ($M)', fontsize=12)
# Remove top and right spines
sns.despine()
plt.tight_layout()
plt.show()Common Palettes
| Palette | Best For |
|---|---|
| deep | Default, good for most |
| pastel | Soft, easy on eyes |
| dark | Bold, high contrast |
| Set2 | Categorical data |
| Blues/Greens | Sequential data |
| coolwarm | Diverging data |
Key Points
- set_theme(style=) changes background
- set_palette() changes colors
- font_scale changes text size
- despine() removes borders
- Set theme once at start of script
- Use reset_defaults() to reset
What's Next?
Congratulations! You've completed Data Visualization I. Next module covers interactive charts with Plotly.