Styling the visualization in Seaborn
Built-in Themes
Seaborn has five built-in themes to style its plots: darkgrid
, whitegrid
, dark
, white
, and ticks
. Seaborn defaults to using the darkgrid
theme for its plots, but you can change this styling to better suit your presentation needs.
To use any of the preset themes pass the name of it to sns.set_style()
.
sns.set_style("darkgrid")
sns.stripplot(x="day", y="total_bill", data=tips)
Despine
In addition to changing the color background, you can also define the usage of spines. Spines are the borders of the figure that contain the visualization. By default, an image has four spines.
You may want to remove some or all of the spines for various reasons. A figure with the left and bottom spines resembles traditional graphs. You can automatically take away the top and right spines using the sns.despine()
function. Note: this function must be called after you have called your plot.
sns.set_style("white")
sns.stripplot(x="day", y="total_bill", data=tips)
sns.despine()
Not including any spines at all may be an aesthetic decision. You can also specify how many spines you want to include by calling despine()
and passing in the spines you want to get rid of, such as: left, bottom, top, right.
sns.set_style("whitegrid")
sns.stripplot(x="day", y="total_bill", data=tips)
sns.despine(left=True, bottom=True)
You can build color palettes using the function sns.color_palette()
. This function can take any of the Seaborn built-in palettes (see below). You can also build your own palettes by passing in a list of colors in any valid Matplotlib format, including RGB tuples, hex color codes, or HTML color names.
If you want to quickly see what a palette looks like, use the function sns.palplot()
to plot a palette as an array of colors:
# Save a palette to a variable:
palette = sns.color_palette("bright")
# Use palplot and pass in the variable:
sns.palplot(palette)
Selecting Color Palettes for Your Dataset
Qualitative Palettes for Categorical Datasets
When using a dataset that uses distinct but non-ordered categories, it’s good to use qualitative palettes. Qualitative palettes are sets of distinct colors which make it easy to distinguish the categories when plotted but don’t imply any particular ordering or meaning.
An example of categorical data is breed of dog. Each of these values, such as Border Collie or Poodle, are distinct from each other but there isn’t any inherent order to these categories.
Here’s an example of a qualitative Color Brewer palette:
qualitative_colors = sns.color_palette("Set3", 10)
sns.palplot(qualitative_colors)
Comments
Post a Comment