{ "cells": [ { "cell_type": "markdown", "id": "4d0a972b-52ea-45b6-b8dd-390917ac1387", "metadata": {}, "source": [ "# Controlling text/label placement with `check_overlap`\n", "\n", "The `check_overlap` parameter in `geom_text()` and `geom_label()` is used to prevent overlapping text labels in the same layer. " ] }, { "cell_type": "code", "execution_count": 1, "id": "650e445e-54be-479f-8fc4-ef302951d631", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import pandas as pd\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "affc092b-6d76-4bbb-8367-f8bea23eab3f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "77a440e3-9a31-4801-afa7-b48bbde42e6e", "metadata": {}, "outputs": [ { "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", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
\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", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mpg = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "mpg.head(3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "d6e76c84-8880-42bb-af3a-6b6ce0946f58", "metadata": {}, "outputs": [], "source": [ "p = ggplot(mpg, aes('displ', 'hwy')) + \\\n", " theme(legend_position = \"none\", panel_background=element_rect(fill=\"#CCCCCC\")) + \\\n", " ggsize(600, 400)" ] }, { "cell_type": "markdown", "id": "22b89ce5-9984-4678-87ea-f99c3cb2e61a", "metadata": {}, "source": [ "**Without** `check_overlap`: The default behavior plots all labels, which can result in an overcrowded plot." ] }, { "cell_type": "code", "execution_count": 5, "id": "9b388360-298f-422d-a95f-3fafb45b2d43", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_text(aes(label='class', color='class'))" ] }, { "cell_type": "markdown", "id": "ccebad73-9cc7-4456-ae99-feaad68d7191", "metadata": {}, "source": [ "**With** `check_overlap=True`: Text labels that overlap existing labels are not rendered.\n", "The labels are processed in the order they appear in the data frame, and if a subsequent\n", "label would overlap with a previous one, it is omitted." ] }, { "cell_type": "code", "execution_count": 6, "id": "31bb72c6-9844-4f41-9a9a-317b3fb53a31", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_text(aes(label='class', color='class'), check_overlap=True)" ] }, { "cell_type": "markdown", "id": "6d0eba37-70ea-466c-a8b0-c8d4647e9633", "metadata": {}, "source": [ "`check_overlap` is also applicable for `geom_label`." ] }, { "cell_type": "code", "execution_count": 7, "id": "c355d808-c269-4d26-89a7-5f48a2496424", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_label(aes(label='class'), check_overlap=True)" ] } ], "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.9.20" } }, "nbformat": 4, "nbformat_minor": 5 }