One of Seaborn's significant advantages is its ability to produce visually appealing plots with minimal effort. A large part of this comes from its built-in themes, which control the overall aesthetic properties like background color, grid lines, and font choices. Seaborn separates these controls into two main categories: styles and contexts.
Styles primarily affect the aesthetics of the plot background, grid, and axis elements (like spines and ticks). Seaborn comes with several predefined styles that you can easily switch between. The most common way to set a style globally for all subsequent plots in your session or script is using the set_theme()
function, specifying the style
parameter.
The five preset styles available are:
darkgrid
: A gray background with white grid lines (often the default). Good for plots with many data points.whitegrid
: A white background with gray grid lines. Similar to darkgrid
but with a cleaner look for some applications.dark
: A dark gray background without grid lines.white
: A plain white background without grid lines. Closest to the standard Matplotlib appearance but often with subtle improvements.ticks
: A white background with ticks added to the axes, which can help in judging values, especially if grid lines are absent.Let's see how to apply a style. First, we need our standard imports and some simple data:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Generate some simple data
x = np.linspace(0, 10, 50)
y = np.sin(x)
# Default Matplotlib plot (for comparison)
plt.figure() # Start a new figure
plt.plot(x, y)
plt.title("Default Matplotlib Style")
plt.show()
# Now, set a Seaborn style and plot again
sns.set_theme(style="whitegrid") # Apply the 'whitegrid' style
plt.figure() # Start another new figure
plt.plot(x, y)
plt.title("Seaborn 'whitegrid' Style")
plt.show()
# Try another style
sns.set_theme(style="ticks") # Apply the 'ticks' style
plt.figure()
plt.plot(x, y)
plt.title("Seaborn 'ticks' Style")
plt.show()
# Reset to default theme parameters (useful for notebooks)
# sns.reset_defaults()
Notice how sns.set_theme(style=...)
changes the appearance of the plots created after it's called. The 'whitegrid'
style adds a white background and helpful grid lines, while 'ticks'
adds small markers along the axes. Choosing a style depends on your data and where the plot will be displayed.
If you only want to apply a style temporarily for a specific plot, you can use the axes_style()
function within a Python with
statement. This avoids changing the global setting.
# Generate data
x = np.linspace(0, 10, 50)
y = np.cos(x)
# Set a global style first (e.g., darkgrid)
sns.set_theme(style="darkgrid")
# Plot with the global style
plt.figure()
plt.plot(x, y)
plt.title("Global Style ('darkgrid')")
plt.show()
# Temporarily use 'white' style for one plot
with sns.axes_style("white"):
plt.figure()
plt.plot(x, y)
plt.title("Temporary Style ('white')")
plt.show()
# Plot again - it uses the global 'darkgrid' style
plt.figure()
plt.plot(x, y)
plt.title("Back to Global Style ('darkgrid')")
plt.show()
While styles control the aesthetic type (grid, background), contexts control the scale of the plot elements. This includes the size of labels, line widths, and marker sizes. Contexts are designed to help you adapt your plots for different situations, such as whether the plot will be used in a presentation slide (talk
, poster
) or embedded in a document (paper
, notebook
).
The four preset contexts, ordered from smallest to largest scaling, are:
paper
: Smallest elements, suitable for inclusion in written documents.notebook
: The default context, balanced for interactive notebook use.talk
: Larger elements, suitable for presentations where viewers might be further away.poster
: Largest elements, for displays viewed from a significant distance.You can set the context using the context
parameter within set_theme()
, or by using the separate set_context()
function. Like styles, set_context()
applies globally, while plotting_context()
can be used within a with
statement for temporary changes.
Let's see the effect of different contexts:
# Generate data
x = np.linspace(0, 10, 50)
y = np.sin(x) * np.exp(x/10)
# Use the default 'notebook' context (often implicitly set)
sns.set_theme(style="whitegrid", context="notebook") # Explicitly set notebook
plt.figure()
plt.plot(x, y)
plt.title("Context: 'notebook' (Default)")
plt.xlabel("X-axis Label")
plt.ylabel("Y-axis Label")
plt.show()
# Switch to the 'paper' context
sns.set_theme(style="whitegrid", context="paper")
plt.figure()
plt.plot(x, y)
plt.title("Context: 'paper'")
plt.xlabel("X-axis Label")
plt.ylabel("Y-axis Label")
plt.show()
# Switch to the 'talk' context
sns.set_theme(style="whitegrid", context="talk")
plt.figure()
plt.plot(x, y)
plt.title("Context: 'talk'")
plt.xlabel("X-axis Label")
plt.ylabel("Y-axis Label")
plt.show()
# Reset theme to defaults if needed
# sns.reset_defaults()
Observe how the 'paper'
context makes the labels, title, and line thickness smaller, while the 'talk'
context makes them significantly larger compared to the default 'notebook'
context. The underlying plot data remains the same, only the presentation scale changes.
You can, and often will, set both the style and context simultaneously using set_theme()
:
sns.set_theme(style="darkgrid", context="talk")
plt.figure()
plt.plot(x, y**2) # Plotting something slightly different
plt.title("Style: 'darkgrid', Context: 'talk'")
plt.xlabel("X-axis")
plt.ylabel("Y-axis Squared")
plt.show()
This combination gives you fine control over both the base aesthetics (style) and the scaling (context) of your Seaborn visualizations, allowing you to quickly tailor plots for their intended audience and medium. Experimenting with these settings is often the first step in customizing your Seaborn plots. For even more detailed control over specific plot elements (like font sizes or specific colors), Seaborn allows overriding parameters via an rc
dictionary, but set_theme
provides a convenient way to manage the overall look and feel.
© 2025 ApX Machine Learning