Download notebook (.ipynb)

Tooltip Customization#

A few examples of tooltip configuring in Lets-Plot via the ‘tooltips’ parameter and ‘theme()’ function.

import numpy as np
import pandas as pd

from lets_plot import *
LetsPlot.setup_html()
mpg_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
print(mpg_df.shape)
mpg_df.head()
(234, 12)
Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy fl class
0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
3 4 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
p = ggplot(mpg_df, aes(x='displ', y='cty', fill='drv', size='hwy')) + \
    scale_size(range=[5, 15], breaks=[15, 40]) + \
    ggsize(600, 350)
# Default tooltips.
p + geom_point(shape=21)
# No tooltips.
p + geom_point(shape=21, tooltips="none")
# Change format for the "size" aesthetic which is already shown in the tooltip by default.
p + geom_point(shape=21,
               tooltips=layer_tooltips().format('^size', '{.0f} mpg'))
# Show the vehicle "class" value in the tooltip (instead of the value of the "size" aesthetic).
p + geom_point(shape=21,
               tooltips=layer_tooltips().line('@class'))
# Configure a multiline tooltip.
p + geom_point(shape=21,
               tooltips=layer_tooltips()
                       .format('cty', '.0f')
                       .format('hwy', '.0f')
                       .format('drv', '{}wd')
                       .format('@year', 'd')
                       .line('@manufacturer @model')
                       .line('cty/hwy [mpg]|@cty/@hwy')
                       .line('@|@class')
                       .line('drive train|@drv')
                       .line('@|@year')) 
# List of variables to place in a multiline tooltip with the default formatting
p + geom_point(shape=21,
               tooltips=layer_tooltips(['manufacturer', 'model', 'class', 'year']).format('@year', 'd')) 
# Define the format for the variable from the list and specify an additional line
p + geom_point(shape=21,
               tooltips=layer_tooltips(['manufacturer', 'model', 'class', 'drv'])
                       .format('drv', '{}wd')
                       .line('cty/hwy [mpg]|@cty/@hwy')) 
# Anchor the tooltip in the top-right corner of the plot.
p + geom_point(shape=21,
               tooltips=layer_tooltips()
                       .anchor('top_right')
                       .min_width(180)
                       .format('cty', '.0f')
                       .format('hwy', '.0f')
                       .format('drv', '{}wd')
                       .format('@year', 'd')
                       .line('@manufacturer @model')
                       .line('cty/hwy [mpg]|@cty/@hwy')
                       .line('@|@class')
                       .line('drive train|@drv')
                       .line('@|@year'))

Side tooltips#

p2 = ggplot(mpg_df, aes('class','hwy')) + \
    theme(legend_position='none') + \
    ggsize(600, 350)

# Default tooltips
p2 + geom_boxplot()
# Configure text in side tootips using the 'format()' function.
p2 + geom_boxplot(tooltips=layer_tooltips()
                          .format('^Y', '{.0f}')       # all Y-positionals (note: no 'labels')
                          .format('^middle', '.2f')    # different precision for 'middle' (note: default 'label')
                          .format('^ymin', 'min: {}')  # ymin/ymax aesthetics:
                          .format('^ymax', 'max: {}')) #  - add custom 'label'; keep formatting that is current default (i.e. '.0f')
# Replace side tooltips with an anchored "general" tooltip.
# The 'line()' function assigns aesthetic or 'variable' to a general multiline tooltip.
p2 + geom_boxplot(tooltips=layer_tooltips()
                          .anchor('top_center')
                          .format('^Y', '.0f')
                          .format('^middle', '.2f')
                          .line('@|^middle')
                          .line('lower/upper|^lower/^upper')
                          .line('min/max|^ymin/^ymax'))

Showing constants in tooltip#

def get_data(*, seed=42):
    np.random.seed(seed)
    return {'x': np.append(np.random.normal(0, 1, 100), np.random.normal(3, 1, 100)),
            'y': np.append(np.random.normal(0, 1, 100), np.random.normal(3, 1, 100))}

data = get_data()
# By default tooltip never shows values defined via layer parameters (constants).
# Still, these values can be added to a layer tooltip using the 'layer_tooltips()' function.
ggplot(data, aes('x', 'y')) + \
    geom_point() + \
    geom_vline(xintercept=np.mean(data['x']), color="red", linetype="dashed", size=1,
               tooltips=layer_tooltips()
                   .format('^xintercept', '.4f')
                   .line('mean = ^xintercept'))

Some other examples#

iris_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv")
print(iris_df.shape)
iris_df.head()
(150, 5)
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
# Default density plot.
ggplot(iris_df) + \
    geom_area(aes(x='sepal_length', fill='species'), 
              stat='density', 
              color='white') + \
    ggsize(650, 300)
# Change tooltips content and move to the corner
ggplot(iris_df) + \
    geom_area(aes(x='sepal_length', fill='species'), 
              stat='density',
              color='white',
              tooltips=layer_tooltips()
                      .anchor('top_right')
                      .line('^fill')
                      .line('length|^x')
                      .line('density|^y')) + \
    ggsize(650, 300)
# Use '..density..' variable in the tooltip
ggplot(iris_df) + \
    geom_area(aes(x='sepal_length', fill='species'), 
              stat='density',
              color='white',
              tooltips=layer_tooltips()
                      .anchor('top_right')
                      .format('..density..', '.4f')
                      .line('density|@..density..')) + \
    ggsize(650, 300)