While creating plots with good data representation is essential, the overall look and feel significantly impact how your visualization is perceived and understood. Seaborn provides a straightforward way to control the general appearance of your plots using pre-defined themes and styles. These themes handle aspects like background color, grid lines, font styles, and axis ticks, allowing you to quickly switch between different aesthetics without manually adjusting numerous Matplotlib parameters.
Think of Seaborn themes as style templates for your plots. Applying a theme instantly changes the default settings for subsequent plots you create in your session or script.
Seaborn comes with several attractive built-in themes. The most common ones are:
darkgrid
: A gray background with white grid lines (often the default). Good for plots where reading specific values off the axes is helpful.whitegrid
: A white background with gray grid lines. Similar to darkgrid
but with a cleaner, brighter look.dark
: A dark gray background with no grid lines.white
: A plain white background with no grid lines. Often preferred for publications.ticks
: Similar to white
, but adds small tick marks on the axes.You can set the theme globally for your entire script or notebook using the sns.set_theme()
function. It's common practice to call this near the beginning of your script, right after importing the libraries.
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.linspace(0, 10, 50)
y1 = np.sin(x)
y2 = np.cos(x)
# Set the 'whitegrid' theme
sns.set_theme(style="whitegrid")
# Create a plot - it will use the 'whitegrid' style
plt.figure(figsize=(6, 4)) # Control figure size
sns.lineplot(x=x, y=y1, label='sin(x)')
sns.lineplot(x=x, y=y2, label='cos(x)')
plt.title("Plot with 'whitegrid' Theme")
plt.xlabel("X-value")
plt.ylabel("Y-value")
plt.legend()
plt.show()
# Now, let's try the 'ticks' theme
sns.set_theme(style="ticks")
# Recreate the plot - it will now use the 'ticks' style
plt.figure(figsize=(6, 4))
sns.lineplot(x=x, y=y1, label='sin(x)')
sns.lineplot(x=x, y=y2, label='cos(x)')
plt.title("Plot with 'ticks' Theme")
plt.xlabel("X-value")
plt.ylabel("Y-value")
plt.legend()
plt.show()
Notice how changing just one line (sns.set_theme(style=...)
) significantly altered the appearance of the plot, including the background, grid, and axis spines.
Sometimes, you might want to apply a specific style to only one or a few plots, rather than changing the global default. Seaborn allows this using a Python with
statement and the sns.axes_style()
function. This context manager temporarily changes the style settings only within the with
block.
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.linspace(0, 10, 50)
y1 = np.sin(x)
# Assume the default or a previously set theme is active
sns.set_theme(style="darkgrid") # Setting a baseline theme
# Plot 1 using the default 'darkgrid' style
plt.figure(figsize=(6, 4))
sns.lineplot(x=x, y=y1)
plt.title("Plot with Default Theme ('darkgrid')")
plt.show()
# Plot 2 using a temporary 'white' style
with sns.axes_style("white"):
plt.figure(figsize=(6, 4))
sns.lineplot(x=x, y=y1)
plt.title("Plot Temporarily Styled as 'white'")
plt.show()
# Plot 3 back to the default 'darkgrid' style
plt.figure(figsize=(6, 4))
sns.lineplot(x=x, y=y1)
plt.title("Plot Back to Default Theme ('darkgrid')")
plt.show()
This approach is useful for maintaining a consistent default style while occasionally deviating for specific visualizations.
While the built-in themes provide excellent starting points, you might want finer control.
Basic Customizations Still Apply: Remember that Seaborn builds on Matplotlib. You can still use Matplotlib functions like plt.title()
, plt.xlabel()
, plt.ylabel()
, plt.xlim()
, plt.ylim()
, and arguments within Seaborn functions (like color
, linestyle
) to customize elements after applying a theme.
sns.set_theme()
Parameters: The set_theme()
function accepts other arguments besides style
. For instance, palette
controls the default color sequence (covered previously), and font_scale
multiplies the size of all text elements (axis labels, titles, legends), which is handy for making text larger or smaller globally.
# Example: Use 'whitegrid' style with larger fonts
sns.set_theme(style="whitegrid", font_scale=1.5)
plt.figure(figsize=(6, 4))
sns.lineplot(x=x, y=y1, label='sin(x)')
sns.lineplot(x=x, y=y2, label='cos(x)')
plt.title("Plot with Scaled Fonts")
plt.show()
# Reset to default scale for subsequent plots if needed
sns.set_theme(style="whitegrid", font_scale=1.0)
sns.set_style()
for Detailed Control: If you want to modify specific elements of a style (like the exact color of the background or grid lines), you can pass a dictionary to sns.set_style()
. This uses Matplotlib's runtime configuration (rc) parameters. Finding the right parameter names can sometimes require looking at the Matplotlib documentation, so this is a more advanced technique usually employed when the standard themes aren't quite right.
# Example: Start with 'darkgrid' but change the background color
custom_style = sns.axes_style("darkgrid")
custom_style['axes.facecolor'] = '#e9ecef' # A light gray from our palette
sns.set_style(custom_style)
plt.figure(figsize=(6, 4))
sns.lineplot(x=x, y=y1, label='sin(x)')
plt.title("Plot with Custom Background")
plt.show()
# Reset to a standard theme afterwards if needed
sns.set_theme(style="whitegrid")
For beginners, sticking to the main style
names ('whitegrid'
, 'dark'
, etc.) and using font_scale
is often sufficient.
The best theme depends on your data and where the plot will be used:
darkgrid
/ whitegrid
: Useful during exploration or when precise value reading aided by the grid is important.white
/ ticks
: Often preferred for reports, presentations, or publications where a clean, less cluttered look is desired. The lack of a grid puts more focus on the data pattern itself.dark
: Can be visually appealing, especially in dark-themed presentations or dashboards, but ensure sufficient contrast for readability.Experiment with different themes to see which one best suits your visualization goals. Applying a theme is a quick first step in polishing your plots before moving on to more specific customizations or saving them for sharing.
© 2025 ApX Machine Learning