In [1]:
%matplotlib inline
from ggplot import *

Colors

ggplot comes with a variety of "scales" that allow you to theme your plots and make them easier to interpret. In addition to the deafult color schemes that ggplot provides, there are also several color scales which allow you to specify more targeted "palettes" of colors to use in your plots.

scale_color_brewer

scale_color_brewer provides sets of colors that are optimized for displaying data on maps. It comes from Cynthia Brewer's aptly named Color Brewer. Lucky for us, these palettes also look great on plots that aren't maps.


In [2]:
ggplot(aes(x='carat', y='price', color='clarity'), data=diamonds) +\
    geom_point() +\
    scale_color_brewer(type='qual')


Out[2]:
<ggplot: (284426949)>

In [3]:
ggplot(aes(x='carat', y='price', color='clarity'), data=diamonds) + \
    geom_point() + \
    scale_color_brewer(type='seq')


Out[3]:
<ggplot: (285216509)>

In [4]:
ggplot(aes(x='carat', y='price', color='clarity'), data=diamonds) + \
    geom_point() + \
    scale_color_brewer(type='seq', palette=4)


Out[4]:
<ggplot: (281095657)>

In [5]:
ggplot(aes(x='carat', y='price', color='clarity'), data=diamonds) + \
    geom_point() + \
    scale_color_brewer(type='div', palette=5)


Out[5]:
<ggplot: (284426921)>

scale_color_gradient

scale_color_gradient allows you to create gradients of colors that can represent a spectrum of values. For instance, if you're displaying temperature data, you might want to have lower values be blue, hotter values be red, and middle values be somewhere in between. scale_color_gradient will calculate the colors each point should be--even those in between colors.


In [6]:
import pandas as pd
temperature = pd.DataFrame({"celsius": range(-88, 58)})
temperature['farenheit'] = temperature.celsius*1.8 + 32
temperature['kelvin'] = temperature.celsius + 273.15

ggplot(temperature, aes(x='celsius', y='farenheit', color='kelvin')) + \
    geom_point() + \
    scale_color_gradient(low='blue', high='red')


Out[6]:
<ggplot: (285218565)>

In [7]:
ggplot(aes(x='x', y='y', color='z'), data=diamonds.head(1000)) +\
    geom_point() +\
    scale_color_gradient(low='red', high='white')


Out[7]:
<ggplot: (291312649)>

In [8]:
ggplot(aes(x='x', y='y', color='z'), data=diamonds.head(1000)) +\
    geom_point() +\
    scale_color_gradient(low='#05D9F6', high='#5011D1')


Out[8]:
<ggplot: (291640013)>

In [9]:
ggplot(aes(x='x', y='y', color='z'), data=diamonds.head(1000)) +\
    geom_point() +\
    scale_color_gradient(low='#E1FA72', high='#F46FEE')


Out[9]:
<ggplot: (291798497)>

scale_color_manual

Want to just specify the colors yourself? No problem, just use scale_color_manual. Add it to your plot as a layer and specify the colors you'd like using a list.


In [10]:
my_colors = [
    "#ff7f50",
    "#ff8b61",
    "#ff9872",
    "#ffa584",
    "#ffb296",
    "#ffbfa7",
    "#ffcbb9",
    "#ffd8ca",
    "#ffe5dc",
    "#fff2ed"
]
ggplot(aes(x='carat', y='price', color='clarity'), data=diamonds) + \
    geom_point() + \
    scale_color_manual(values=my_colors)


Out[10]:
<ggplot: (291633405)>

In [11]:
# https://coolors.co/app/69a2b0-659157-a1c084-edb999-e05263
ggplot(aes(x='carat', y='price', color='cut'), data=diamonds) + \
    geom_point() + \
    scale_color_manual(values=['#69A2B0', '#659157', '#A1C084', '#EDB999', '#E05263'])


Out[11]:
<ggplot: (291987845)>

In [ ]: