0
# Styling and Themes
1
2
Configure plot aesthetics, themes, color palettes, and plotting contexts with extensive customization options for publication-quality graphics. These functions provide comprehensive control over the visual appearance of seaborn and matplotlib plots.
3
4
## Capabilities
5
6
### Theme Configuration
7
8
Set overall aesthetic parameters and make seaborn the default matplotlib style.
9
10
```python { .api }
11
def set_theme(
12
context="notebook",
13
style="darkgrid",
14
palette="deep",
15
font="sans-serif",
16
font_scale=1,
17
color_codes=True,
18
rc=None
19
):
20
"""
21
Set multiple theme parameters in one step.
22
23
Parameters:
24
- context: str, plotting context ("paper", "notebook", "talk", "poster")
25
- style: str, axes style ("darkgrid", "whitegrid", "dark", "white", "ticks")
26
- palette: str or list, color palette
27
- font: str, font family
28
- font_scale: float, scaling factor for font sizes
29
- color_codes: bool, use seaborn color codes
30
- rc: dict, matplotlib rcParams to override
31
"""
32
33
def set(
34
context="notebook",
35
style="darkgrid",
36
palette="deep",
37
font="sans-serif",
38
font_scale=1,
39
color_codes=True,
40
rc=None
41
):
42
"""
43
Alias for set_theme() - sets aesthetic parameters in one step.
44
"""
45
```
46
47
### Style Management
48
49
Control the appearance of plot axes and backgrounds.
50
51
```python { .api }
52
def axes_style(style=None, rc=None):
53
"""
54
Get the parameters that control the general style of the plots.
55
56
Parameters:
57
- style: str, style name ("darkgrid", "whitegrid", "dark", "white", "ticks")
58
- rc: dict, parameter overrides
59
60
Returns:
61
dict of matplotlib rcParams
62
63
Can be used as context manager:
64
with sns.axes_style("white"):
65
# plots with white background
66
"""
67
68
def set_style(style, rc=None):
69
"""
70
Set the aesthetic style of the plots.
71
72
Parameters:
73
- style: str, style name or dict of parameters
74
- rc: dict, parameter overrides
75
"""
76
```
77
78
### Plotting Context
79
80
Control the scale of plot elements for different contexts.
81
82
```python { .api }
83
def plotting_context(context=None, font_scale=1, rc=None):
84
"""
85
Get the parameters that control the scaling of plot elements.
86
87
Parameters:
88
- context: str, context name ("paper", "notebook", "talk", "poster")
89
- font_scale: float, scaling factor for font sizes
90
- rc: dict, parameter overrides
91
92
Returns:
93
dict of matplotlib rcParams
94
95
Can be used as context manager:
96
with sns.plotting_context("poster"):
97
# plots scaled for poster presentation
98
"""
99
100
def set_context(context, font_scale=1, rc=None):
101
"""
102
Set the plotting context parameters.
103
104
Parameters:
105
- context: str, context name or dict of parameters
106
- font_scale: float, scaling factor for font sizes
107
- rc: dict, parameter overrides
108
"""
109
```
110
111
### Palette Management
112
113
Set and retrieve color palettes for consistent color schemes.
114
115
```python { .api }
116
def set_palette(palette, n_colors=None, desat=None, color_codes=False):
117
"""
118
Set the matplotlib color cycle using a seaborn palette.
119
120
Parameters:
121
- palette: str, list, or dict, palette specification
122
- n_colors: int, number of colors in cycle
123
- desat: float, desaturation factor (0-1)
124
- color_codes: bool, use seaborn color codes
125
"""
126
127
def color_palette(palette=None, n_colors=None, desat=None, as_cmap=False):
128
"""
129
Return a list of colors or continuous colormap defining a palette.
130
131
Parameters:
132
- palette: str, list, or None, palette specification
133
- n_colors: int, number of colors to return
134
- desat: float, desaturation factor (0-1)
135
- as_cmap: bool, return matplotlib Colormap object
136
137
Returns:
138
list of RGB tuples or matplotlib Colormap
139
"""
140
```
141
142
### Reset Functions
143
144
Restore default settings.
145
146
```python { .api }
147
def reset_defaults():
148
"""Restore all seaborn style settings to default values."""
149
150
def reset_orig():
151
"""Restore all matplotlib rcParams to original settings before seaborn import."""
152
```
153
154
### Color Code Configuration
155
156
Control interpretation of matplotlib color abbreviations.
157
158
```python { .api }
159
def set_color_codes(palette="deep"):
160
"""
161
Change how matplotlib color shorthands are interpreted.
162
163
Parameters:
164
- palette: str, palette name for color codes
165
166
Calling this changes the meaning of abbreviations like 'b', 'g', 'r', etc.
167
"""
168
```
169
170
## Available Styles
171
172
### Axes Styles
173
174
- **"darkgrid"**: Dark background with white grid lines (default)
175
- **"whitegrid"**: White background with gray grid lines
176
- **"dark"**: Dark background without grid
177
- **"white"**: White background without grid
178
- **"ticks"**: White background with ticks but no grid
179
180
### Plotting Contexts
181
182
- **"paper"**: Smallest scale, for journal figures
183
- **"notebook"**: Medium scale, for Jupyter notebooks (default)
184
- **"talk"**: Larger scale, for presentations
185
- **"poster"**: Largest scale, for posters
186
187
## Usage Examples
188
189
### Basic Theme Setup
190
191
```python
192
import seaborn as sns
193
import matplotlib.pyplot as plt
194
195
# Set overall theme
196
sns.set_theme(style="whitegrid", palette="pastel")
197
198
# Create a plot - will use theme settings
199
tips = sns.load_dataset("tips")
200
sns.boxplot(data=tips, x="day", y="total_bill")
201
plt.show()
202
```
203
204
### Style Context Manager
205
206
```python
207
# Temporarily change style
208
with sns.axes_style("white"):
209
sns.scatterplot(data=tips, x="total_bill", y="tip")
210
plt.show()
211
212
# Back to default style
213
sns.boxplot(data=tips, x="day", y="total_bill")
214
plt.show()
215
```
216
217
### Context Scaling
218
219
```python
220
# Scale for poster presentation
221
sns.set_context("poster", font_scale=1.2)
222
sns.barplot(data=tips, x="day", y="total_bill")
223
plt.show()
224
225
# Reset to notebook context
226
sns.set_context("notebook")
227
```
228
229
### Custom Palette
230
231
```python
232
# Set custom color palette
233
custom_colors = ["#FF6B6B", "#4ECDC4", "#45B7D1", "#FFA07A"]
234
sns.set_palette(custom_colors)
235
236
# Plots will use custom colors
237
sns.countplot(data=tips, x="day")
238
plt.show()
239
```
240
241
### Color Codes
242
243
```python
244
# Enable seaborn color codes
245
sns.set_color_codes("muted")
246
247
# Now 'b', 'g', 'r' use seaborn colors
248
plt.plot([1, 2, 3], [1, 4, 2], 'b-') # Uses seaborn blue
249
plt.show()
250
```
251
252
### Context Manager for Scaling
253
254
```python
255
# Temporarily scale for presentation
256
with sns.plotting_context("talk", font_scale=1.5):
257
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
258
259
sns.scatterplot(data=tips, x="total_bill", y="tip", ax=axes[0,0])
260
sns.boxplot(data=tips, x="day", y="total_bill", ax=axes[0,1])
261
sns.histplot(data=tips, x="total_bill", ax=axes[1,0])
262
sns.barplot(data=tips, x="day", y="total_bill", ax=axes[1,1])
263
264
plt.tight_layout()
265
plt.show()
266
```
267
268
### Reset to Defaults
269
270
```python
271
# Reset all seaborn settings
272
sns.reset_defaults()
273
274
# Or reset to pre-seaborn matplotlib settings
275
sns.reset_orig()
276
```
277
278
### Fine-tuned Theme
279
280
```python
281
# Detailed theme customization
282
sns.set_theme(
283
context="notebook",
284
style="ticks",
285
palette="Set2",
286
font="serif",
287
font_scale=1.1,
288
rc={
289
"axes.spines.right": False,
290
"axes.spines.top": False,
291
"axes.linewidth": 1.2,
292
"grid.alpha": 0.4
293
}
294
)
295
296
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="smoker")
297
plt.show()
298
```
299
300
## Types
301
302
```python { .api }
303
# Style options
304
AxesStyle = Literal["darkgrid", "whitegrid", "dark", "white", "ticks"]
305
PlottingContext = Literal["paper", "notebook", "talk", "poster"]
306
307
# Palette specifications
308
PaletteSpec = str | list | dict | None
309
310
# RC parameters
311
RCParams = dict[str, Any] # matplotlib rcParams dictionary
312
```