In [1]:
import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt
%matplotlib inline

import pandas as pd
import seaborn as sns

In [2]:
import sys
print(sys.version)


3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:52:12) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)]

In [3]:
!python --version


Python 3.5.2 :: Continuum Analytics, Inc.

Prepare a minimal data set.


In [4]:
dataframe = pd.DataFrame({'age':[1.,2,3,4,5,6,7,8,9],
                         'height':[4, 4.5, 5, 6, 7, 8, 9, 9.5, 10],
                         'gender':['M','F', 'F','M','M','F', 'F','M', 'F'],
                         #'hair color':['brown','black', 'brown', 'blonde', 'brown', 'red', 
                         #              'brown', 'brown', 'black' ],
                         'hair length':[1,6,2,3,1,5,6,5,3] })

In [5]:
dataframe


Out[5]:
age gender hair length height
0 1.0 M 1 4.0
1 2.0 F 6 4.5
2 3.0 F 2 5.0
3 4.0 M 3 6.0
4 5.0 M 1 7.0
5 6.0 F 5 8.0
6 7.0 F 6 9.0
7 8.0 M 5 9.5
8 9.0 F 3 10.0

You can pass seaborn color palettes of RGB values.

For example:


In [6]:
sns.color_palette(palette="Blues_d", n_colors=9, desat=None)


Out[6]:
[(0.20307574244885662, 0.26459054565118034, 0.30867615307078644),
 (0.20627451459566754, 0.33176471312840783, 0.42169935226440436),
 (0.20935025704452415, 0.3963552587795881, 0.53037550533519073),
 (0.21254902919133503, 0.46352942625681565, 0.64339870452880865),
 (0.21747534201036089, 0.53052930169635348, 0.75482251877878226),
 (0.30692811806996667, 0.58640524546305339, 0.78352942466735842),
 (0.39995900517195659, 0.64451622698042133, 0.81338460679147762),
 (0.48941178123156226, 0.70039217074712123, 0.84209151268005367),
 (0.58244266833355229, 0.75850315226448906, 0.87194669480417286)]

The following plot uses a seaborn-derived color pallette. See below for info about how to make, modify, and view them.


In [7]:
def plot_data():
    color_palette = sns.color_palette(palette="Blues_d", n_colors=9, desat=None)
    g = sns.FacetGrid(dataframe, col="gender", hue='height', palette=color_palette)
    g = (g.map(plt.scatter, "age", 'hair length', edgecolor="w").add_legend())

plot_data()



In [8]:
# change seaborn style
sns.set(style="ticks")

# re-make the plot with this setting. 
plot_data()


Add some settings to matplotlib to get bigger fonts.

Note: not all of their suggestions are appropriate for my distribution/needs.


In [9]:
# settings from I heart pandas: 
# https://github.com/fredhutchio/i-heart-pandas/blob/master/3-pandas.ipynb
mpl.rcParams.update({
    'font.size': 16, 'axes.titlesize': 17, 'axes.labelsize': 15,
    'xtick.labelsize': 10, 'ytick.labelsize': 13,
    #'font.family': 'Lato',  # I don't have this font family
        'font.weight': 600,
    'axes.labelweight': 600, 'axes.titleweight': 600,
    #'figure.autolayout': True  # screws up layout for seaborn facetgrid objects. 
    })

In [10]:
# re-make the plot with this setting. 
plot_data()


Making custom color maps

You can pass seaborn custom RGB values. Just put the lists of RGB values in a list:


In [11]:
# a sample seaborn-created color palette: 
# http://stanford.edu/~mwaskom/software/seaborn-dev/tutorial/color_palettes.html 

sns.diverging_palette(145, 280, s=85, l=25, n=7)


Out[11]:
[array([ 0.07106475,  0.2657334 ,  0.17177439,  1.        ]),
 array([ 0.36441028,  0.51184576,  0.44068437,  1.        ]),
 array([ 0.66442276,  0.76355158,  0.71570594,  1.        ]),
 array([ 0.95,  0.95,  0.95,  1.  ]),
 array([ 0.7629515 ,  0.6726194 ,  0.84602388,  1.        ]),
 array([ 0.54270509,  0.38706233,  0.68583934,  1.        ]),
 array([ 0.32735306,  0.10785097,  0.52921445,  1.        ])]

Seaborn also comes with a function called palplot, which can display color palettes.


In [12]:
sns.palplot(sns.diverging_palette(145, 280, s=85, l=25, n=7))


Since the palettes are just lists of list, you can path them together as you like.


In [13]:
sns.palplot(sns.diverging_palette(145, 280, s=85, l=25, n=7)[0:5] +
            sns.diverging_palette(220, 20, n=7)[4:])



In [ ]: