Plot Styling and Scaling of Elements
Table of Contents
Plot styling
Seaborn splits Matplotlib parameters into two independent groups: The first group sets the aesthetic style of the plot and second group scales various elements of the figure.
Let us first see how to customize the look and style of the plots. Seaborn has five built-in themes to style its plots – darkgrid, whitegrid, dark, white, and ticks. The darkgrid is the default theme, but we can change this style to suit our requirements.
We can customize the styles such as background color, color of tick marks, text color, font type etc., using the functions axes_style() and set_style(). Both these functions take same set of arguments.
The axes_style() function defines and returns a dictionary of rc parameters related to the styling of the plots. This function returns an object that can be used in a with statement to temporarily change the style parameters.
The set_style() function is used to set the aesthetic style of the plots, the rc parameters can be customized using this function.
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
Below we see the rc parameters returned by the axes_style function. The default settings of the parameters can be changed to fine tune the look of the plots.
sns.axes_style()
{'axes.facecolor': 'white',
'axes.edgecolor': 'black',
'axes.grid': False,
'axes.axisbelow': 'line',
'axes.labelcolor': 'black',
'figure.facecolor': (1, 1, 1, 0),
'grid.color': '#b0b0b0',
'grid.linestyle': '-',
'text.color': 'black',
'xtick.color': 'black',
'ytick.color': 'black',
'xtick.direction': 'out',
'ytick.direction': 'out',
'lines.solid_capstyle': 'projecting',
'patch.edgecolor': 'black',
'image.cmap': 'viridis',
'font.family': ['sans-serif'],
'font.sans-serif': ['DejaVu Sans',
'Bitstream Vera Sans',
'Computer Modern Sans Serif',
'Lucida Grande',
'Verdana',
'Geneva',
'Lucid',
'Arial',
'Helvetica',
'Avant Garde',
'sans-serif'],
'patch.force_edgecolor': False,
'xtick.bottom': True,
'xtick.top': False,
'ytick.left': True,
'ytick.right': False,
'axes.spines.left': True,
'axes.spines.bottom': True,
'axes.spines.right': True,
'axes.spines.top': True}
Scatter Plot
A scatterplot is used to display the correlation between two numerical variables. The values of both the variables are displayed with dots. Each dot on the scatterplot represents one observation from the data set.
We will plot a scatter plot which uses the ‘tips’ dataset to display the tips received on the total_bill, both of which are quantitative variables. The scatter plot is rendered in default theme which is darkgrid style.
tips = sns.load_dataset('tips')
sns.scatterplot('total_bill','tip',data=tips)
plt.title('Default theme - Darkgrid')
plt.show()

As you can see, the default theme has a light grey background with white gridlines.
Let’s look at another example, in the scatter plot function below, we have passed the ‘ticks’ theme to the style parameter. The ‘ticks’ theme allows the colors of the dataset to show more visibly. Apart from this, we have also changed the axes edgecolor, text color, ticks color in the plot by changing the default rc parameter values.
sns.set_style(style='ticks',rc={'axes.edgecolor': 'b','text.color': 'r','xtick.color': 'r','ytick.color': 'r'})
sns.scatterplot('total_bill','tip',data=tips)
plt.title('Ticks theme')
plt.show()

We can temporarily change the style parameters of a plot by using the axes_style function in a with statement as shown in the example below.
with sns.axes_style(style='whitegrid',rc={'font.family': 'serif','font.serif':'Times'}):
sns.scatterplot('total_bill','tip',data=tips)
plt.title('Whitegrid theme')
plt.show()

Scaling of plot elements
Next we will see how to scale the various elements in the plot. Seaborn has four preset contexts which set the size of the plot and allow us to customize the plot depending on how it will be presented. The four preset contexts, in order of relative size are – paper, notebook, talk and poster. The notebook style is the default context, which can be changed depending on our requirement.
We can customize the size of the plot elements such as labels,ticks,markers,linewidth etc., using the functions plotting_context() and set_context(). Both these functions take same set of arguments.
The plotting_context() function defines and returns a dictionary of rc parameters related to plot elements such as label size,tick size,marker size. This function returns an object that can be used in a with statement to temporarily change the context parameters.
The set_context() function is used to set the plotting context parameters.
Below we see the rc parameters returned by the plotting_context function. The default settings of the parameters can be changed to scale plot elements.
sns.plotting_context()
{'font.size': 12.0,
'axes.labelsize': 12.0,
'axes.titlesize': 12.0,
'xtick.labelsize': 11.0,
'ytick.labelsize': 11.0,
'legend.fontsize': 11.0,
'axes.linewidth': 1.25,
'grid.linewidth': 1.0,
'lines.linewidth': 1.5,
'lines.markersize': 6.0,
'patch.linewidth': 1.0,
'xtick.major.width': 1.25,
'ytick.major.width': 1.25,
'xtick.minor.width': 1.0,
'ytick.minor.width': 1.0,
'xtick.major.size': 6.0,
'ytick.major.size': 6.0,
'xtick.minor.size': 4.0,
'ytick.minor.size': 4.0}
The scatter plot below uses the ‘tips’ dataset to display the tips received on the total_bill. The scatter plot is rendered in the notebook context which is the default context.
sns.set()
sns.scatterplot('total_bill','tip',data=tips)
plt.title('Notebook context')
plt.show()

In the example below, we have passed ‘talk’ to the context parameter. Apart from this, we have also changed the label size, title size, grid linewidth of the plot by changing the default rc parameter values.
sns.set_context(context='talk',rc={'axes.labelsize': 20.0,'axes.titlesize': 20.0,'grid.linewidth': 2.5})
sns.scatterplot('total_bill','tip',data=tips)
plt.title('Talk context')
plt.show()

We can temporarily change the context parameters of a plot by using the plotting_context function in a with statement as shown in the example below.
with sns.plotting_context(context='poster'):
sns.scatterplot('total_bill','tip',data=tips)
plt.title('Poster context')
plt.show()

set () function
The set() function in Seaborn sets the style and context parameters in one step, see example below.
sns.set(style='white',context='paper',rc={'axes.edgecolor': 'b','axes.titlesize': 20.0})
sns.scatterplot('total_bill','tip',data=tips)
plt.title('Paper context')
plt.show()

If you want to switch to Seaborn default settings, then call the set() function without passing any arguments.
Default settings:
- context=’notebook’
- style=’darkgrid’
- palette=’deep’
- font=’sans-serif’
- font_scale=1
- color_codes=True
sns.set()
sns.scatterplot('total_bill','tip',data=tips)
plt.title('Plot with default settings')
plt.show()

despine() function
Spines are the borders on the sides of a graph or plot. By default, a plot has four spines. The despine() function can be used to remove the spines in the plot, by default the top and right spines are removed using this function.
sns.set(style='ticks')
sns.scatterplot('total_bill','tip',data=tips)
plt.title('Plot with four spines/borders')
plt.show()

sns.set(style='ticks')
sns.scatterplot('total_bill','tip',data=tips)
sns.despine()
plt.title('Plot with top and right spines removed')
plt.show()

You can choose to remove all the spines if you think they are unnecessary and distracting, see example below.
sns.set(style='white')
sns.scatterplot('total_bill','tip',data=tips)
sns.despine(left=True,bottom=True)
plt.title('Plot with all spines removed')
plt.show()
