Download notebook (.ipynb)

Named System Colors#

import numpy as np

from lets_plot import *
LetsPlot.setup_html()
LetsPlot.set_theme(theme_grey())

1. Named System Colors#

In Lets-Plot, there are 3 system theme colors - pen, brush and paper.

  • pen - the main drawing color of the current theme flavor

  • paper - the plot background of the current theme flavor

  • brush - the color often used to fill shapes

data = {
    'name': ['pen', 'brush', 'paper'],
    'slice': [1, 3, 3]
}
pie_plot = ggplot(data) + \
    geom_pie(aes(fill='name', slice='slice'),
             stat='identity',
             color='pen',
             tooltips='none', labels=layer_labels().line('@name')) + \
    scale_fill_manual(['pen', 'brush', 'paper'])

pie_plot

1.1. Named Colors With Theme Flavor#

Let’s see at the pie chart how the named system colors look like with different theme flavors.

gggrid([
    pie_plot + ggtitle('default'),
    pie_plot + flavor_darcula() + ggtitle('darcula'),
    pie_plot + flavor_solarized_light() + ggtitle('solarized_light'),
    pie_plot + flavor_solarized_dark() + ggtitle('solarized_dark'),
    pie_plot + flavor_high_contrast_light() + ggtitle('high_contrast_light'),
    pie_plot + flavor_high_contrast_dark() + ggtitle('high_contrast_dark')
], ncol=2)

1.2. Reassign System Colors#

System colors can be reassigned via parameter geom in theme():

theme(geom=element_geom(pen=None, brush=None, paper=None)

where function element_geom allows to specify new values for the named colors.

pie_plot + \
    theme(geom=element_geom(pen='dark_green', brush='light_green', paper='yellow'))

theme() + flavor()

pie_plot + \
    theme(geom=element_geom(pen='dark_green', brush='light_green', paper='yellow')) + \
    flavor_solarized_light()

2. Theme Colors for Geometries#

The default presentation of geometry uses pen, brush and paper colors.

  • pen - a hight-contrast color commonly used to draw dots and lines

  • brush - a color we often use to fill shapes

  • paper - a background color we often use to fill shapes as well

2.1. Typical Defaults#

System colors were designed in a way that the shapes are clearly visible with any theme flavor applied.

def get_line_data(size):
    x = np.linspace(-4 * np.pi, 4 * np.pi, size)
    y = np.sin(x)
    return {'x': x, 'y': y}

def get_bar_data(size, seed=42):
    np.random.seed(seed)
    return {'x': np.random.randint(10, size=size)}

def get_box_data(size, seed=42):
    np.random.seed(seed)
    return {'x': np.random.choice(['a', 'b', 'c'], size=size),
            'y': np.random.normal(size=size)}

line_data = get_line_data(100)
bar_data = get_bar_data(100)
box_data = get_box_data(100)
line_plot = ggplot(line_data, aes(x='x', y='y')) + \
    geom_line() + \
    ggtitle("color: 'pen'")
bar_plot = ggplot(bar_data, aes(x='x')) + \
    geom_bar() + \
    ggtitle("fill: 'brush'")
box_plot = ggplot(box_data, aes(x='x', y='y')) + \
    geom_boxplot() + \
    ggtitle("fill: 'paper'")

gggrid([line_plot, bar_plot, box_plot])

2.2. Apply Theme Colors Manually#

Feel free to use “pen”, “brush” or “paper” literals to customize individual geometries.

custom_bar_plot = ggplot(bar_data, aes('x')) + \
    geom_bar(color='pen', fill='paper') + \
    theme(axis_title_x='blank')

gggrid([
    custom_bar_plot,
    custom_bar_plot + flavor_solarized_light()
]) + ggsize(800, 200)

2.3. Reassign Geometry Theme Colors#

You can reassign geometry theme colors using element_geom() function in theme:

theme(geom=element_geom(pen='red', brush='green', paper='blue')

orange_theme = theme(geom=element_geom(pen='#ff4500', brush='#ff7f50', paper='#ffcba4'))
pie_plot + orange_theme
gggrid([
    line_plot + orange_theme,
    bar_plot + orange_theme,
    box_plot + orange_theme
])