{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "7fc7ce70", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "\n", "import pandas as pd\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "b6519902", "metadata": {}, "outputs": [], "source": [ "# Support of multiple lines\n", "\n", "d = {\n", " 'hjust': [0, 0.5, 1],\n", " 'vjust': [0, 0.5, 1],\n", " 'angle': [0, 30],\n", " 'label': [\"first line\\nsecond line\"]\n", "}\n", "\n", "from itertools import product\n", "td = pd.DataFrame(product(*d.values()), columns=d.keys())\n", "\n", "p = (ggplot(td, aes(x='hjust', y='vjust')) + \n", " geom_point(size=3) +\n", " theme_light() + theme(panel_grid=element_blank())\n", ")\n", "\n", "pf = (p + scale_x_continuous(breaks=[0, 0.5, 1]) +\n", " scale_y_continuous(breaks=[0, 0.5, 1], expand=[0.4]) +\n", " facet_grid(x='angle', x_format='{d}°'))\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "cc9b5c4e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pf + geom_text(aes(label='label', hjust='hjust', vjust='vjust', angle='angle'), size=9)" ] }, { "cell_type": "code", "execution_count": 4, "id": "cafa06a0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pf + geom_label(aes(label='label', hjust='hjust', vjust='vjust', angle='angle'), size=9, alpha=0.5)" ] }, { "cell_type": "code", "execution_count": 5, "id": "4f32ccb6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change lineheight\n", "\n", "# lineheight = 0.7\n", "\n", "p + geom_text(aes(label='label', hjust='hjust', vjust='vjust'), size=9, lineheight=0.7)" ] }, { "cell_type": "code", "execution_count": 6, "id": "33749388", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_label(aes(label='label', hjust='hjust', vjust='vjust'), size=9, alpha=0.5, lineheight=0.7)" ] }, { "cell_type": "code", "execution_count": 7, "id": "edb7a3f7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# lineheight = 2.0\n", "\n", "p + geom_text(aes(label='label', hjust='hjust', vjust='vjust'), size=9, lineheight=2)" ] }, { "cell_type": "code", "execution_count": 8, "id": "39adabb1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_label(aes(label='label', hjust='hjust', vjust='vjust'), size=9, alpha=0.5, lineheight=2)" ] }, { "cell_type": "code", "execution_count": null, "id": "03f5e591", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 9, "id": "fd3890df", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Adjust position by nudging a given offset\n", "\n", "p2 = ggplot({'x': ['a', 'b', 'c'], 'y': [1.2, 3.4, 2.5]}, aes('x', 'y')) + geom_point(size=4) + ggsize(500, 300)\n", "\n", "p2 + geom_text(aes(label = 'y'))" ] }, { "cell_type": "code", "execution_count": 10, "id": "151debf1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Move text - use 'position_nudge'\n", "\n", "p2 + geom_text(aes(label = 'y'), position=position_nudge(y=0.2))" ] }, { "cell_type": "code", "execution_count": 11, "id": "3451eed6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Same with 'nudge_y'\n", "\n", "p2 + geom_text(aes(label = 'y'), nudge_y=0.2)" ] }, { "cell_type": "code", "execution_count": 12, "id": "1ae37db7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Justification: 'inward' and 'outward'\n", "\n", "df = {\n", " 'x' : [1, 1, 2, 2, 1.5],\n", " 'y' : [1, 2, 1, 2, 1.5],\n", " 'text': [\"bottom-left\", \"top-left\", \"bottom-right\", \"top-right\", \"center\"]\n", "}\n", "\n", "\n", "p3 = ggplot(df, aes('x', 'y')) + geom_point(size=4) + ggsize(500, 300)\n", "p3 + geom_text(aes(label = 'text'), size = 8)" ] }, { "cell_type": "code", "execution_count": 13, "id": "e180acc7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p3 + geom_text(aes(label = 'text'), size = 8, hjust = 'inward', vjust = 'inward')" ] }, { "cell_type": "code", "execution_count": 14, "id": "b94a483b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p3 + geom_text(aes(label = 'text'), size = 8, hjust = 'outward', vjust = 'outward')" ] }, { "cell_type": "code", "execution_count": 15, "id": "e43299f1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# livemap: geom_label parameters and multiple lines support\n", "data = {\n", " 'city': ['New York City', 'Singapore'],\n", " 'lon': [-73.7997, 104.0012],\n", " 'lat': [40.6408, 1.3256],\n", "}\n", "\n", "\n", "ggplot(data, aes(x='lon', y='lat')) + \\\n", " geom_livemap(geodesic=False, projection='epsg4326', symbol='point', color='white',\n", " tiles=maptiles_lets_plot(theme='dark')) + \\\n", " geom_path(color='white') + \\\n", " geom_label(aes(label='city'), fill='black', color='white',\n", " size=8, angle=10, hjust=0, vjust=1,\n", " label_padding=0.6, label_r=0.5, label_size=1.5) + \\\n", " geom_text(\n", " x=40, y=50,\n", " label=\"First flight: November 9th 2020\\n\" + \n", " \"Flight distance: 15,349 km\\n\" + \n", " \"Flight time: 18 Hours, 50 Minutes\\n\" + \n", " \"Aircraft: Airbus A350-900ULR\",\n", " size=7, hjust=0, lineheight=2, color='white')" ] } ], "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.7.13" } }, "nbformat": 4, "nbformat_minor": 5 }