sns12 - Controlling Figure Aesthetics
sns12 - Controlling Figure Aesthetics
Drawing attractive figures is important. When making figures for yourself, as you explore a dataset, it’s nice to have plots that are pleasant to
look at. Visualizations are also central to communicating quantitative insights to an audience, and in that setting it’s even more necessary to
have figures that catch the attention and draw a viewer in.
Matplotlib is highly customizable, but it can be hard to know what settings to tweak to achieve an attractive plot. Seaborn comes with a
number of customized themes and a high-level interface for controlling the look of matplotlib figures.
Let’s define a simple function to plot some offset sine waves, which will help us see the different stylistic parameters we can tweak.
Seaborn splits matplotlib parameters into two independent groups. The first group sets the aesthetic style of the plot, and the second scales
various elements of the figure so that it can be easily incorporated into different contexts.
The interface for manipulating these parameters are two pairs of functions. To control the style, use the axes_style() and set_style()
functions. To scale the plot, use the plotting_context() and set_context() functions. In both cases, the first function returns a dictionary of
parameters and the second sets the matplotlib defaults.
For many plots, (especially for settings like talks, where you primarily want to use figures to provide impressions of patterns in the data), the
grid is less necessary.
Sometimes you might want to give a little extra structure to the plots, which is where ticks come in handy:
sns.set_style("ticks")
sinplot()
f, ax = plt.subplots()
sns.violinplot(data=data)
sns.despine(offset=10, trim=True);
You can also control which spines are removed with additional arguments to despine() :
sns.set_style("whitegrid")
sns.boxplot(data=data, palette="deep")
sns.despine(left=True)
with sns.axes_style("whitegrid"):
ax = f.add_subplot(gs[1, 1])
sinplot(6)
f.tight_layout()
If you want to see what parameters are included, you can just call the function with no arguments, which will return the current settings:
The four preset contexts, in order of relative size, are paper , notebook , talk , and poster . The notebook style is the default, and was used in
the plots above.
Controlling figure aesthetics — seaborn 0.13.2 documentation https://seaborn.pydata.org/tutorial/aesthetics.html
Most of what you now know about the style functions should transfer to the context functions.
You can call set_context() with one of these names to set the parameters, and you can override the parameters by providing a dictionary of
parameter values.
You can also independently scale the size of the font elements when changing the context. (This option is also available through the top-level
set() function).
Similarly, you can temporarily control the scale of figures nested under a with statement.
Both the style and the context can be quickly configured with the set() function. This function also sets the default color palette, but that will
be covered in more detail in the next section of the tutorial.
7 of 7 09/02/2024, 06:10