{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tooltip Customization\n", "\n", "A few examples of tooltip configuring in *Lets-Plot* via the 'tooltips' parameter and 'theme()' function. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.352254Z", "iopub.status.busy": "2026-01-27T17:04:26.352159Z", "iopub.status.idle": "2026-01-27T17:04:26.354895Z", "shell.execute_reply": "2026-01-27T17:04:26.354678Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.355968Z", "iopub.status.busy": "2026-01-27T17:04:26.355897Z", "iopub.status.idle": "2026-01-27T17:04:26.357442Z", "shell.execute_reply": "2026-01-27T17:04:26.357218Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.370297Z", "iopub.status.busy": "2026-01-27T17:04:26.370221Z", "iopub.status.idle": "2026-01-27T17:04:26.477815Z", "shell.execute_reply": "2026-01-27T17:04:26.477245Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(234, 12)\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
34audia42.020084auto(av)f2130pcompact
45audia42.819996auto(l5)f1626pcompact
\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n", "3 4 audi a4 2.0 2008 4 auto(av) f 21 30 \n", "4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 \n", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact \n", "3 p compact \n", "4 p compact " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mpg_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "print(mpg_df.shape)\n", "mpg_df.head()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.478880Z", "iopub.status.busy": "2026-01-27T17:04:26.478800Z", "iopub.status.idle": "2026-01-27T17:04:26.480968Z", "shell.execute_reply": "2026-01-27T17:04:26.480751Z" } }, "outputs": [], "source": [ "p = ggplot(mpg_df, aes(x='displ', y='cty', fill='drv', size='hwy')) + \\\n", " scale_size(range=[5, 15], breaks=[15, 40]) + \\\n", " ggsize(600, 350)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.481802Z", "iopub.status.busy": "2026-01-27T17:04:26.481726Z", "iopub.status.idle": "2026-01-27T17:04:26.513901Z", "shell.execute_reply": "2026-01-27T17:04:26.513625Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Default tooltips.\n", "p + geom_point(shape=21)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.514834Z", "iopub.status.busy": "2026-01-27T17:04:26.514760Z", "iopub.status.idle": "2026-01-27T17:04:26.519702Z", "shell.execute_reply": "2026-01-27T17:04:26.519314Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# No tooltips.\n", "p + geom_point(shape=21, tooltips=\"none\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.520350Z", "iopub.status.busy": "2026-01-27T17:04:26.520279Z", "iopub.status.idle": "2026-01-27T17:04:26.525200Z", "shell.execute_reply": "2026-01-27T17:04:26.524874Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change format for the \"size\" aesthetic which is already shown in the tooltip by default.\n", "p + geom_point(shape=21,\n", " tooltips=layer_tooltips().format('^size', '{.0f} mpg'))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.525830Z", "iopub.status.busy": "2026-01-27T17:04:26.525757Z", "iopub.status.idle": "2026-01-27T17:04:26.530882Z", "shell.execute_reply": "2026-01-27T17:04:26.530622Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show the vehicle \"class\" value in the tooltip (instead of the value of the \"size\" aesthetic).\n", "p + geom_point(shape=21,\n", " tooltips=layer_tooltips().line('@class'))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.531594Z", "iopub.status.busy": "2026-01-27T17:04:26.531523Z", "iopub.status.idle": "2026-01-27T17:04:26.537602Z", "shell.execute_reply": "2026-01-27T17:04:26.537352Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Configure a multiline tooltip.\n", "p + geom_point(shape=21,\n", " tooltips=layer_tooltips()\n", " .format('cty', '.0f')\n", " .format('hwy', '.0f')\n", " .format('drv', '{}wd')\n", " .format('@year', 'd')\n", " .line('@manufacturer @model')\n", " .line('cty/hwy [mpg]|@cty/@hwy')\n", " .line('@|@class')\n", " .line('drive train|@drv')\n", " .line('@|@year')) " ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.538341Z", "iopub.status.busy": "2026-01-27T17:04:26.538269Z", "iopub.status.idle": "2026-01-27T17:04:26.544037Z", "shell.execute_reply": "2026-01-27T17:04:26.543788Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# List of variables to place in a multiline tooltip with the default formatting\n", "p + geom_point(shape=21,\n", " tooltips=layer_tooltips(['manufacturer', 'model', 'class', 'year']).format('@year', 'd')) " ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.544767Z", "iopub.status.busy": "2026-01-27T17:04:26.544692Z", "iopub.status.idle": "2026-01-27T17:04:26.550554Z", "shell.execute_reply": "2026-01-27T17:04:26.550269Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Define the format for the variable from the list and specify an additional line\n", "p + geom_point(shape=21,\n", " tooltips=layer_tooltips(['manufacturer', 'model', 'class', 'drv'])\n", " .format('drv', '{}wd')\n", " .line('cty/hwy [mpg]|@cty/@hwy')) " ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.551194Z", "iopub.status.busy": "2026-01-27T17:04:26.551125Z", "iopub.status.idle": "2026-01-27T17:04:26.556906Z", "shell.execute_reply": "2026-01-27T17:04:26.556618Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Anchor the tooltip in the top-right corner of the plot.\n", "p + geom_point(shape=21,\n", " tooltips=layer_tooltips()\n", " .anchor('top_right')\n", " .min_width(180)\n", " .format('cty', '.0f')\n", " .format('hwy', '.0f')\n", " .format('drv', '{}wd')\n", " .format('@year', 'd')\n", " .line('@manufacturer @model')\n", " .line('cty/hwy [mpg]|@cty/@hwy')\n", " .line('@|@class')\n", " .line('drive train|@drv')\n", " .line('@|@year'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Side tooltips" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.557532Z", "iopub.status.busy": "2026-01-27T17:04:26.557462Z", "iopub.status.idle": "2026-01-27T17:04:26.562856Z", "shell.execute_reply": "2026-01-27T17:04:26.562595Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 = ggplot(mpg_df, aes('class','hwy')) + \\\n", " theme(legend_position='none') + \\\n", " ggsize(600, 350)\n", "\n", "# Default tooltips\n", "p2 + geom_boxplot()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.563577Z", "iopub.status.busy": "2026-01-27T17:04:26.563505Z", "iopub.status.idle": "2026-01-27T17:04:26.568384Z", "shell.execute_reply": "2026-01-27T17:04:26.568119Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Configure text in side tootips using the 'format()' function.\n", "p2 + geom_boxplot(tooltips=layer_tooltips()\n", " .format('^Y', '{.0f}') # all Y-positionals (note: no 'labels')\n", " .format('^middle', '.2f') # different precision for 'middle' (note: default 'label')\n", " .format('^ymin', 'min: {}') # ymin/ymax aesthetics:\n", " .format('^ymax', 'max: {}')) # - add custom 'label'; keep formatting that is current default (i.e. '.0f')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.569072Z", "iopub.status.busy": "2026-01-27T17:04:26.568999Z", "iopub.status.idle": "2026-01-27T17:04:26.573952Z", "shell.execute_reply": "2026-01-27T17:04:26.573702Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Replace side tooltips with an anchored \"general\" tooltip.\n", "# The 'line()' function assigns aesthetic or 'variable' to a general multiline tooltip.\n", "p2 + geom_boxplot(tooltips=layer_tooltips()\n", " .anchor('top_center')\n", " .format('^Y', '.0f')\n", " .format('^middle', '.2f')\n", " .line('@|^middle')\n", " .line('lower/upper|^lower/^upper')\n", " .line('min/max|^ymin/^ymax'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Showing constants in tooltip" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.574682Z", "iopub.status.busy": "2026-01-27T17:04:26.574610Z", "iopub.status.idle": "2026-01-27T17:04:26.576459Z", "shell.execute_reply": "2026-01-27T17:04:26.576230Z" } }, "outputs": [], "source": [ "def get_data(*, seed=42):\n", " np.random.seed(seed)\n", " return {'x': np.append(np.random.normal(0, 1, 100), np.random.normal(3, 1, 100)),\n", " 'y': np.append(np.random.normal(0, 1, 100), np.random.normal(3, 1, 100))}\n", "\n", "data = get_data()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.577336Z", "iopub.status.busy": "2026-01-27T17:04:26.577262Z", "iopub.status.idle": "2026-01-27T17:04:26.581762Z", "shell.execute_reply": "2026-01-27T17:04:26.581477Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# By default tooltip never shows values defined via layer parameters (constants).\n", "# Still, these values can be added to a layer tooltip using the 'layer_tooltips()' function.\n", "ggplot(data, aes('x', 'y')) + \\\n", " geom_point() + \\\n", " geom_vline(xintercept=np.mean(data['x']), color=\"red\", linetype=\"dashed\", size=1,\n", " tooltips=layer_tooltips()\n", " .format('^xintercept', '.4f')\n", " .line('mean = ^xintercept'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Some other examples" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.582512Z", "iopub.status.busy": "2026-01-27T17:04:26.582435Z", "iopub.status.idle": "2026-01-27T17:04:26.841697Z", "shell.execute_reply": "2026-01-27T17:04:26.841323Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(150, 5)\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "0 5.1 3.5 1.4 0.2 setosa\n", "1 4.9 3.0 1.4 0.2 setosa\n", "2 4.7 3.2 1.3 0.2 setosa\n", "3 4.6 3.1 1.5 0.2 setosa\n", "4 5.0 3.6 1.4 0.2 setosa" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iris_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv\")\n", "print(iris_df.shape)\n", "iris_df.head()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.842700Z", "iopub.status.busy": "2026-01-27T17:04:26.842626Z", "iopub.status.idle": "2026-01-27T17:04:26.861942Z", "shell.execute_reply": "2026-01-27T17:04:26.861707Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Default density plot.\n", "ggplot(iris_df) + \\\n", " geom_area(aes(x='sepal_length', fill='species'), \n", " stat='density', \n", " color='white') + \\\n", " ggsize(650, 300)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.862929Z", "iopub.status.busy": "2026-01-27T17:04:26.862855Z", "iopub.status.idle": "2026-01-27T17:04:26.879458Z", "shell.execute_reply": "2026-01-27T17:04:26.879246Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change tooltips content and move to the corner\n", "ggplot(iris_df) + \\\n", " geom_area(aes(x='sepal_length', fill='species'), \n", " stat='density',\n", " color='white',\n", " tooltips=layer_tooltips()\n", " .anchor('top_right')\n", " .line('^fill')\n", " .line('length|^x')\n", " .line('density|^y')) + \\\n", " ggsize(650, 300)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:04:26.880433Z", "iopub.status.busy": "2026-01-27T17:04:26.880358Z", "iopub.status.idle": "2026-01-27T17:04:26.897759Z", "shell.execute_reply": "2026-01-27T17:04:26.897531Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use '..density..' variable in the tooltip\n", "ggplot(iris_df) + \\\n", " geom_area(aes(x='sepal_length', fill='species'), \n", " stat='density',\n", " color='white',\n", " tooltips=layer_tooltips()\n", " .anchor('top_right')\n", " .format('..density..', '.4f')\n", " .line('density|@..density..')) + \\\n", " ggsize(650, 300)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 4 }