Lets-Plot Themes

In [1]:
import numpy as np
import pandas as pd
from PIL import Image

from lets_plot import *
from lets_plot.geo_data import *
from lets_plot.bistro.corr import *
LetsPlot.setup_html()
The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).

Preparation

In [2]:
w, h = 400, 300
In [3]:
df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
gdf = geocode_countries().get_boundaries(3)
img = Image.open("images/fisher_boat.png")
In [4]:
p_plot = ggplot(df) + geom_point(aes('cty', 'hwy'))
p_plot
Out[4]:
In [5]:
b_plot = ggplot(df) + geom_bar(aes('fl', color='fl', fill='fl'))
b_plot
Out[5]:
In [6]:
f_plot = b_plot + facet_grid(x='year')
f_plot
Out[6]:
In [7]:
c_plot = corr_plot(df, flip=False).points().build()
c_plot
Out[7]:
In [8]:
g_plot = ggplot() + geom_map(data=gdf, color='white', fill='black')
g_plot
Out[8]:
In [9]:
i_plot = ggplot() + geom_image(image_data=np.asarray(img))
i_plot
Out[9]:

Elements

In [10]:
e_blank = element_blank()
e_line = element_line(color='#de2d26', size=4)
e_line2 = element_line(color='#fcae91', size=1)
e_rect = element_rect(color='#2c7fb8', fill='#edf8b1', size=2)
e_text = element_text(color='#31a354')

Themes

In [11]:
bunch = GGBunch()
bunch.add_plot(p_plot + ggtitle("default (theme_minimal2())"), \
               0, 0, w, h)
bunch.add_plot(p_plot + ggtitle("theme_light()") + theme_light(), \
               w, 0, w, h)
bunch.add_plot(p_plot + ggtitle("theme_classic()") + theme_classic(), \
               0, h, w, h)
bunch.add_plot(p_plot + ggtitle("theme_grey()") + theme_grey(), \
               w, h, w, h)
bunch.add_plot(p_plot + ggtitle("theme_minimal()") + theme_minimal(), \
               0, 2 * h, w, h)
bunch.add_plot(p_plot + ggtitle("theme_none()") + theme_none(), \
               w, 2 * h, w, h)
bunch.show()

Theme Parameters

In [12]:
bunch = GGBunch()
for i, (param, element, element_name) in enumerate([('axis_title_x', e_text, 'e_text'), \
                                                    ('axis_text_x', e_text, 'e_text'), \
                                                    ('axis_ticks_x', e_line, 'e_line'), \
                                                    ('axis_line_x', e_line, 'e_line')]):
    bunch.add_plot(p_plot + ggtitle("{0}=element_blank".format(param)) + theme(**{param: element_blank()}), \
                   0, i * h, w, h)
    bunch.add_plot(p_plot + ggtitle("{0}={1}".format(param, element_name)) + theme(**{param: element}), \
                   w, i * h, w, h)
bunch.show()
In [13]:
b_plot + \
    ggtitle("Place legend") + \
    theme(legend_position=[1, 1], legend_justification=[1, 1], legend_direction='vertical')
Out[13]:
In [14]:
bunch = GGBunch()
bunch.add_plot(p_plot + ggtitle("axis_tooltip_x=e_blank") + theme(axis_tooltip_x=e_blank), \
               0, 0, w, h)
bunch.add_plot(p_plot + ggtitle("axis_tooltip_x=e_rect") + theme(axis_tooltip_x=e_rect), \
               w, 0, w, h)
bunch.show()
In [15]:
bunch = GGBunch()
bunch.add_plot(p_plot + ggtitle("default"), \
               0, 0, w, h)
bunch.add_plot(p_plot + ggtitle("line=e_line") + theme(line=e_line), \
               w, 0, w, h)
bunch.show()
In [16]:
bunch = GGBunch()
bunch.add_plot(b_plot + ggtitle("theme_none()") + theme_none(), \
               0, 0, w, h)
bunch.add_plot(b_plot + ggtitle("theme_none + rect") + theme_none() + theme(rect=e_rect), \
               w, 0, w, h)
bunch.show()
In [17]:
bunch = GGBunch()
bunch.add_plot(f_plot + ggtitle("default"), \
               0, 0, w, h)
bunch.add_plot(f_plot + ggtitle("text=e_text") + theme(text=e_text), \
               w, 0, w, h)
bunch.add_plot(f_plot + ggtitle("legend_text=e_text") + theme(legend_text=e_text), \
               0, h, w, h)
bunch.add_plot(f_plot + ggtitle("strip_text=e_text") + theme(strip_text=e_text), \
               w, h, w, h)
bunch.show()
In [18]:
bunch = GGBunch()
bunch.add_plot(b_plot + ggtitle("default"), \
               0, 0, w, h)
bunch.add_plot(b_plot + ggtitle("title=e_text") + theme(title=e_text), \
               w, 0, w, h)
bunch.add_plot(b_plot + ggtitle("plot_title=e_text") + theme(plot_title=e_text), \
               0, h, w, h)
bunch.add_plot(b_plot + ggtitle("legend_title=e_text") + theme(legend_title=e_text), \
               w, h, w, h)
bunch.show()
In [19]:
bunch = GGBunch()
bunch.add_plot(p_plot + ggtitle("default"), \
               0, 0, w, h)
bunch.add_plot(p_plot + ggtitle("axis=e_blank") + theme(axis=e_blank), \
               w, 0, w, h)
bunch.add_plot(p_plot + ggtitle("axis=e_line") + theme(axis=e_line), \
               0, h, w, h)
bunch.add_plot(p_plot + ggtitle("axis=e_text") + theme(axis=e_text), \
               w, h, w, h)
bunch.show()
In [20]:
bunch = GGBunch()
bunch.add_plot(p_plot + ggtitle("default"), \
               0, 0, w, h)
bunch.add_plot(p_plot + ggtitle("panel_background=e_rect") + theme(panel_background=e_rect), \
               w, 0, w, h)
bunch.show()
In [21]:
bunch = GGBunch()
bunch.add_plot(b_plot + ggtitle("panel_grid=e_blank") + theme(panel_grid=e_blank), \
               0, 0, w, h)
bunch.add_plot(p_plot + ggtitle("panel_grid=e_line") + theme(panel_grid=e_line), \
               w, 0, w, h)
bunch.add_plot(p_plot + ggtitle("panel_grid_major=e_line, panel_grid_minor=element_line") + \
                   theme(panel_grid_major=e_line, panel_grid_minor=e_line2), \
               0, h, w, h)
bunch.add_plot(b_plot + ggtitle("panel_grid_major_x=e_blank, panel_grid_minor_x=e_blank") + \
               theme(panel_grid_major_x=e_blank, panel_grid_major_y=e_line, \
                     panel_grid_minor_x=e_blank, panel_grid_minor_y=e_line2), \
               w, h, w, h)
bunch.show()
In [22]:
bunch = GGBunch()
bunch.add_plot(f_plot + ggtitle("default") + theme_grey(), \
               0, 0, w, h)
bunch.add_plot(f_plot + ggtitle("strip_background=e_rect") + theme_grey() + theme(strip_background=e_rect), \
               w, 0, w, h)
bunch.add_plot(f_plot + ggtitle("strip_background=e_blank") + theme_grey() + theme(strip_background=e_blank), \
               0, h, w, h)
bunch.add_plot(f_plot + ggtitle("strip_text=e_blank") + theme_grey() + theme(strip_text=e_blank), \
               w, h, w, h)
bunch.show()

Extra

In [23]:
bunch = GGBunch()
bunch.add_plot(b_plot + coord_flip() + ggtitle("default"), \
               0, 0, w, h)
bunch.add_plot(b_plot + coord_flip() + ggtitle("plot 1") + theme(line=e_line, text=e_text), \
               w, 0, w, h)
bunch.add_plot(b_plot + coord_flip() + ggtitle("plot 2") + theme(axis=e_line, legend_title=e_text), \
               0, h, w, h)
bunch.add_plot(b_plot + coord_flip() + ggtitle("plot 3") + theme(plot_title=e_text, \
                                                                 panel_background=e_rect, \
                                                                 panel_grid_major_x=e_blank, \
                                                                 panel_grid_major_y=e_line, \
                                                                 panel_grid_minor_x=e_blank, \
                                                                 panel_grid_minor_y=e_line2), \
               w, h, w, h)
bunch.add_plot(f_plot + coord_flip() + ggtitle("plot 4") + \
               theme(strip_background=e_rect, strip_text=e_text), \
               0, 2 * h, w, h)
bunch.add_plot(b_plot + coord_flip() + ggtitle("plot 5") + theme(axis_title_x=e_text, \
                                                                 axis_text_x=e_text, \
                                                                 axis_ticks_x=e_line, \
                                                                 axis_line_x=e_line2, \
                                                                 axis_tooltip_x=e_blank, \
                                                                 legend_position='bottom', \
                                                                 legend_direction='horizontal'), \
               w, 2 * h, w, h)
bunch.show()
In [24]:
bunch = GGBunch()
bunch.add_plot(c_plot + ggtitle("default"), \
               0, 0, w, h)
bunch.add_plot(c_plot + ggtitle("with theme") + theme(text=e_text, \
                                                      axis=e_line, \
                                                      panel_background=e_rect, \
                                                      axis_title_x=e_text, \
                                                      axis_text_x=e_text, \
                                                      axis_ticks_x=e_line, \
                                                      axis_line_x=e_line, \
                                                      axis_tooltip_x=e_blank), \
               w, 0, w, h)
bunch.show()
In [25]:
bunch = GGBunch()
bunch.add_plot(g_plot + ggtitle("default"), \
               0, 0, w, h)
bunch.add_plot(g_plot + coord_cartesian() + ggtitle("coord_cartesian()"), \
               w, 0, w, h)
bunch.add_plot(g_plot + coord_fixed() + ggtitle("coord_fixed()"), \
               0, h, w, h)
bunch.add_plot(g_plot + ggtitle("theme_classic()") + theme_classic(), \
               w, h, w, h)
bunch.show()
In [26]:
bunch = GGBunch()
bunch.add_plot(i_plot + ggtitle("default"), \
               0, 0, w, h)
bunch.add_plot(i_plot + ggtitle("classic theme") + theme_classic(), \
               w, 0, w, h)
bunch.show()
In [27]:
pt_plot = ggplot(df, aes('cty', 'hwy')) + \
    geom_point(aes(color='fl'), tooltips=layer_tooltips().anchor('top_center').min_width(50))

bunch = GGBunch()
bunch.add_plot(pt_plot + ggtitle("default"), \
               0, 0, w, h)
bunch.add_plot(pt_plot + ggtitle("gray background") + \
               theme(panel_background=element_rect(color='black', fill='gray')), \
               w, 0, w, h)
bunch.show()