{ "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": "2024-04-26T12:09:51.403631Z", "iopub.status.busy": "2024-04-26T12:09:51.403631Z", "iopub.status.idle": "2024-04-26T12:09:52.395965Z", "shell.execute_reply": "2024-04-26T12:09:52.395965Z" } }, "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": "2024-04-26T12:09:52.395965Z", "iopub.status.busy": "2024-04-26T12:09:52.395965Z", "iopub.status.idle": "2024-04-26T12:09:52.411678Z", "shell.execute_reply": "2024-04-26T12:09:52.411678Z" } }, "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": "2024-04-26T12:09:52.427294Z", "iopub.status.busy": "2024-04-26T12:09:52.427294Z", "iopub.status.idle": "2024-04-26T12:09:52.710582Z", "shell.execute_reply": "2024-04-26T12:09:52.710582Z" } }, "outputs": [], "source": [ "# Load mpg dataset.\n", "mpg_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:09:52.710582Z", "iopub.status.busy": "2024-04-26T12:09:52.710582Z", "iopub.status.idle": "2024-04-26T12:09:52.741973Z", "shell.execute_reply": "2024-04-26T12:09:52.741973Z" } }, "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)\n", " ) " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:09:52.744576Z", "iopub.status.busy": "2024-04-26T12:09:52.744576Z", "iopub.status.idle": "2024-04-26T12:09:52.852071Z", "shell.execute_reply": "2024-04-26T12:09:52.852071Z" } }, "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": "2024-04-26T12:09:52.852071Z", "iopub.status.busy": "2024-04-26T12:09:52.852071Z", "iopub.status.idle": "2024-04-26T12:09:52.867909Z", "shell.execute_reply": "2024-04-26T12:09:52.867909Z" } }, "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": "2024-04-26T12:09:52.867909Z", "iopub.status.busy": "2024-04-26T12:09:52.867909Z", "iopub.status.idle": "2024-04-26T12:09:52.883525Z", "shell.execute_reply": "2024-04-26T12:09:52.883525Z" } }, "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": "2024-04-26T12:09:52.883525Z", "iopub.status.busy": "2024-04-26T12:09:52.883525Z", "iopub.status.idle": "2024-04-26T12:09:52.899168Z", "shell.execute_reply": "2024-04-26T12:09:52.899168Z" } }, "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": "2024-04-26T12:09:52.899168Z", "iopub.status.busy": "2024-04-26T12:09:52.899168Z", "iopub.status.idle": "2024-04-26T12:09:52.914637Z", "shell.execute_reply": "2024-04-26T12:09:52.914637Z" } }, "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", " .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": "2024-04-26T12:09:52.914637Z", "iopub.status.busy": "2024-04-26T12:09:52.914637Z", "iopub.status.idle": "2024-04-26T12:09:52.930382Z", "shell.execute_reply": "2024-04-26T12:09:52.930382Z" } }, "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'])) " ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:09:52.930382Z", "iopub.status.busy": "2024-04-26T12:09:52.930382Z", "iopub.status.idle": "2024-04-26T12:09:52.946217Z", "shell.execute_reply": "2024-04-26T12:09:52.946217Z" } }, "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')\n", " ) " ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:09:52.946217Z", "iopub.status.busy": "2024-04-26T12:09:52.946217Z", "iopub.status.idle": "2024-04-26T12:09:52.977652Z", "shell.execute_reply": "2024-04-26T12:09:52.977652Z" } }, "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 \n", " + 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", " .line('@manufacturer @model')\n", " .line('cty/hwy [mpg]|@cty/@hwy')\n", " .line('@|@class')\n", " .line('drive train|@drv')\n", " .line('@|@year')) \n", ") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \"Outlier\" tooltips." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:09:52.977652Z", "iopub.status.busy": "2024-04-26T12:09:52.977652Z", "iopub.status.idle": "2024-04-26T12:09:53.008905Z", "shell.execute_reply": "2024-04-26T12:09:53.008905Z" } }, "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", "\n", "# Default tooltips\n", "p2 + geom_boxplot()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:09:53.008905Z", "iopub.status.busy": "2024-04-26T12:09:53.008905Z", "iopub.status.idle": "2024-04-26T12:09:53.040167Z", "shell.execute_reply": "2024-04-26T12:09:53.040167Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Configure text in outlier tootips using the 'format()' function.\n", "p2 + geom_boxplot(\n", " 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'\n", " ) # - keep formatting that is current default (i.e. '.0f')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:09:53.045318Z", "iopub.status.busy": "2024-04-26T12:09:53.045318Z", "iopub.status.idle": "2024-04-26T12:09:53.071795Z", "shell.execute_reply": "2024-04-26T12:09:53.071795Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Replace \"outlier\" 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": "2024-04-26T12:09:53.071795Z", "iopub.status.busy": "2024-04-26T12:09:53.071795Z", "iopub.status.idle": "2024-04-26T12:09:53.103019Z", "shell.execute_reply": "2024-04-26T12:09:53.103019Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "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", "np.random.seed(42)\n", "\n", "data= {}\n", "data['x'] = np.append(np.random.normal(0,1,100), np.random.normal(3,1,100))\n", "data['y'] = np.append(np.random.normal(0,1,100), np.random.normal(3,1,100))\n", "\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'))\n", ") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Some other examples." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:09:53.103019Z", "iopub.status.busy": "2024-04-26T12:09:53.103019Z", "iopub.status.idle": "2024-04-26T12:09:53.371874Z", "shell.execute_reply": "2024-04-26T12:09:53.371874Z" } }, "outputs": [], "source": [ "# Load the iris dataset\n", "iris_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:09:53.371874Z", "iopub.status.busy": "2024-04-26T12:09:53.371874Z", "iopub.status.idle": "2024-04-26T12:09:53.434581Z", "shell.execute_reply": "2024-04-26T12:09:53.434581Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "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) \n", ")" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:09:53.434581Z", "iopub.status.busy": "2024-04-26T12:09:53.434581Z", "iopub.status.idle": "2024-04-26T12:09:53.481881Z", "shell.execute_reply": "2024-04-26T12:09:53.481881Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "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) \n", ")" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:09:53.481881Z", "iopub.status.busy": "2024-04-26T12:09:53.481881Z", "iopub.status.idle": "2024-04-26T12:09:53.528622Z", "shell.execute_reply": "2024-04-26T12:09:53.528622Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "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", " )\n", " + ggsize(650, 300) \n", ")" ] } ], "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 }