In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns

In [2]:
sns.__version__


Out[2]:
'0.8.1'

1. Controlling figure aesthetics

Let us generate some data to work with.


In [3]:
# Collective data
def sinplot(flip=1):
    x = np.linspace(0, 14, 100)
    for i in range(1, 7):
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)

# Individual data        
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2

Figure style

functions to practice -

  • set_style()
  • axes_style()

In [4]:
# axis parameters
sns.axes_style()


Out[4]:
{'axes.facecolor': 'w',
 'axes.edgecolor': 'k',
 'axes.grid': False,
 'axes.axisbelow': 'line',
 'axes.linewidth': 0.8,
 'axes.labelcolor': 'k',
 'figure.facecolor': (1, 1, 1, 0),
 'grid.color': '#b0b0b0',
 'grid.linestyle': '-',
 'text.color': 'k',
 'xtick.color': 'k',
 'ytick.color': 'k',
 'xtick.direction': 'out',
 'ytick.direction': 'out',
 'xtick.major.size': 3.5,
 'ytick.major.size': 3.5,
 'xtick.minor.size': 2.0,
 'ytick.minor.size': 2.0,
 'legend.frameon': True,
 'legend.numpoints': 1,
 'legend.scatterpoints': 1,
 'lines.solid_capstyle': 'projecting',
 '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']}

In [5]:
sns.set_style(style='whitegrid', 
              rc={'font.sans-setif':'Helvetica'}) # axis parameters can be passed in argument rc
sns.boxplot(data=data)


Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f55b4396780>

In [6]:
# removing top and right axis splines using despline
sns.set_style('white')
sns.boxplot(data=data)
sns.despine() # despine arguments can control which side to be removed



In [7]:
sns.boxplot(data=data)
sns.despine(trim=True)



In [8]:
# axis_style helps to make temporarily changes when used with 'with'
with sns.axes_style('darkgrid'):
    sns.violinplot(data=data)


Resetting seaborn style parameters to default using set()


In [9]:
sns.set_style('white') # user-defined

In [10]:
sns.set() # resetting all style parameters to default

In [11]:
sns.violinplot(data=data) # verifying


Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f55b21f9e48>

2. Choosing color palettes


In [12]:
col = sns.color_palette(palette='hls', n_colors=10)
sns.set_palette(col)
sns.violinplot(data=data)


Out[12]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f55a758c828>

Possible values to be passed inside color palette can be viewed here

Possible values are: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r


In [13]:
# Colors can also be passed in the hex format like: #fff
sns.set_palette(sns.color_palette(["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]))
sns.violinplot(data=data)


Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f55a749c550>

In [14]:
# Sequential color maps
sns.set_palette(sns.color_palette("Blues"))
sns.violinplot(data=data)


Out[14]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f55a741d898>

In [15]:
# Sequential cubehelix color maps - 
# helpful to preserve the info when plots printed in black and white (useful for color-blinds)

sns.set_palette(sns.cubehelix_palette(reverse=True, light=0.95, dark=0.2, hue=0.5, 
                                      rot=0.6,  # an arbitrary value between -1 and 1
                                      start=2)) # an arbitrary value between 0 and 3
sns.violinplot(data=data)


Out[15]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f55a73a9710>

In [16]:
# light palette
sns.set_palette(sns.light_palette((0.22, 0.85, 0.125)))
sns.violinplot(data=data)


Out[16]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f55a733e860>

In [17]:
# dark palette
sns.set_palette(sns.dark_palette((0.22, 0.85, 0.125)))
sns.violinplot(data=data)


Out[17]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f55a72bf7f0>

In [18]:
# divergent palette
sns.set_palette(sns.diverging_palette(8, 2200, n=6)) # First two arguments are in degrees
sns.violinplot(data=data)


Out[18]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f55a723b9e8>