Plots Style and Color

We’ve shown a few times how to control figure aesthetics in seaborn, but let’s now go over it formally:

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
tips = sns.load_dataset('tips')

Styles

You can set particular styles:

sns.countplot(x='sex',data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0x7f1d2f3651d0>

png

sns.set_style('white')
sns.countplot(x='sex',data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0x7f1d2f2bf3c8>

png

sns.set_style('ticks')
sns.countplot(x='sex',data=tips,palette='deep')
<matplotlib.axes._subplots.AxesSubplot at 0x7f1d2f279ef0>

png

Spine Removal

sns.countplot(x='sex',data=tips)
sns.despine()

png

sns.countplot(x='sex',data=tips)
sns.despine(left=True)

png

Size and Aspect

You can use matplotlib’s **plt.figure(figsize=(width,height) ** to change the size of most seaborn plots.

You can control the size and aspect ratio of most seaborn grid plots by passing in parameters: size, and aspect. For example:

# Non Grid Plot
plt.figure(figsize=(12,3))
sns.countplot(x='sex',data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0x7f1d2f4d8b38>

png

# Grid Type Plot
sns.lmplot(x='total_bill',y='tip',height=2,aspect=4,data=tips)
/home/ggilmore/.local/lib/python3.6/site-packages/seaborn/axisgrid.py:375: UserWarning: Tight layout not applied. The bottom and top margins cannot be made large enough to accommodate all axes decorations. 
  fig.tight_layout()
/home/ggilmore/.local/lib/python3.6/site-packages/seaborn/axisgrid.py:848: UserWarning: Tight layout not applied. The bottom and top margins cannot be made large enough to accommodate all axes decorations. 
  self.fig.tight_layout()





<seaborn.axisgrid.FacetGrid at 0x7f1d2f1e7860>

png

Scale and Context

The set_context() allows you to override default parameters:

sns.set_context('poster',font_scale=4)
sns.countplot(x='sex',data=tips,palette='coolwarm')
<matplotlib.axes._subplots.AxesSubplot at 0x7f1d2f1e7b70>

png

Check out the documentation page for more info on these topics: https://stanford.edu/~mwaskom/software/seaborn/tutorial/aesthetics.html

Greydon Gilmore
Greydon Gilmore
Ph.D. Candidate in Biomedical Engineering

My research interests include deep brain stimulation, machine learning and signal processing.

Previous
Next