{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "9ef6598d-3dcf-4297-8010-75f90c7838c6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "from lets_plot import *\n", "from lets_plot.geo_data import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "3aa98090-03d4-4106-9c10-987dd9faffc5", "metadata": {}, "source": [ "#### Without nudge" ] }, { "cell_type": "code", "execution_count": 2, "id": "44c46535-64ac-49d2-9a43-76b44d2aa8f1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + ylim(0, 80) + xlim(-180, 0) + \\\n", " geom_map() + \\\n", " geom_point(x = -73, y = 40, size = 10, color = 'red') + \\\n", " geom_text(x = -73, y = 40, label = 'text') " ] }, { "cell_type": "markdown", "id": "75564153-0761-456f-ad25-21a9f965ecce", "metadata": {}, "source": [ "#### Different units" ] }, { "cell_type": "code", "execution_count": 3, "id": "7ee95dd2-7096-4dc0-86d5-c88f75f072f8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot = ggplot() + ylim(0, 90) + xlim(-180, 0) + \\\n", " geom_point(x = -73, y = 30, size = 5, color = 'red') + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 identity', nudge_y = 50) + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 px', nudge_y = 50, nudge_unit = 'px') + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 size', nudge_y = 50, nudge_unit = 'size')\n", "\n", "gggrid([\n", " plot + ggtitle('Without geom_map'), \n", " plot + geom_map() + ggtitle('With geom_map'),\n", "])" ] }, { "cell_type": "markdown", "id": "343d2dda-6faf-4dcd-95ef-cc95e12b915e", "metadata": {}, "source": [ "#### position_nudge()" ] }, { "cell_type": "code", "execution_count": 4, "id": "a0c7a82f-7e27-4427-9b88-3282349e2944", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + ylim(0, 90) + xlim(-180, 0) + \\\n", " geom_point(x = -73, y = 30, size = 5, color = 'red') + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 identity', position = position_nudge(0, 50)) + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 px', position = position_nudge(0, 50, 'px')) + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 size', position = position_nudge(0, 50, 'size'))" ] }, { "cell_type": "markdown", "id": "db29151e-9aec-4396-9d39-53cab65b3596", "metadata": {}, "source": [ "#### Data from dataframe" ] }, { "cell_type": "code", "execution_count": 5, "id": "190e2cba-3350-4b4c-8c5e-d0af364e75d4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " 'lon': [-73],\n", " 'lat': [30],\n", " 'v': [80],\n", "}\n", "df = pd.DataFrame(data)\n", "ggplot(df) + ylim(0, 90) + xlim(-180, 0) + \\\n", " geom_point(aes(x = 'lon', y = 'lat', size = 'v'), color = 'red') + \\\n", " geom_text(aes(x = 'lon', y = 'lat'), label = 'nudge_y 50 identity', nudge_y = 50) + \\\n", " geom_text(aes(x = 'lon', y = 'lat'), label = 'nudge_y 50 px', nudge_y = 50, nudge_unit = 'px') + \\\n", " geom_text(aes(x = 'lon', y = 'lat'), label = 'nudge_y 50 size', nudge_y = 50, nudge_unit = 'size')" ] }, { "cell_type": "markdown", "id": "013aceaa-78c5-4adc-9df6-c6b759d5fdd9", "metadata": {}, "source": [ "#### Text nudge is half the size of the point. The text is on the edge of the point. The projection does not affect the size." ] }, { "cell_type": "code", "execution_count": 6, "id": "cdb1d00a-d8dc-48dd-91dd-cf3d37c89a4b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot = ggplot() + ylim(0, 90) + xlim(-180, 0) + \\\n", " geom_point(x = -40, y = 30, size = 30, color = 'red') + \\\n", " geom_text(x = -40, y = 30, label = 'size 15', nudge_y = 15, nudge_unit = 'size') + \\\n", " geom_text(x = -40, y = 30, label = 'size -15', nudge_y = -15, nudge_unit = 'size') + \\\n", " geom_point(x = -140, y = 30, size = 30, color = 'red') + \\\n", " geom_text(x = -140, y = 30, label = 'size -15 vjust 1', nudge_y = -15, nudge_unit = 'size', vjust = 1) + \\\n", " geom_text(x = -140, y = 30, label = 'size 15 vjust 0', nudge_y = 15, nudge_unit = 'size', vjust = 0) + \\\n", " geom_point(x = -40, y = 70, size = 30, color = 'red') + \\\n", " geom_text(x = -40, y = 70, label = 'size 15', nudge_y = 15, nudge_unit = 'size') + \\\n", " geom_text(x = -40, y = 70, label = 'size -15', nudge_y = -15, nudge_unit = 'size') + \\\n", " geom_point(x = -140, y = 70, size = 30, color = 'red') + \\\n", " geom_text(x = -140, y = 70, label = 'size -15 vjust 1', nudge_y = -15, nudge_unit = 'size', vjust = 1) + \\\n", " geom_text(x = -140, y = 70, label = 'size 15 vjust 0', nudge_y = 15, nudge_unit = 'size', vjust = 0)\n", "\n", "gggrid([\n", " plot + ggtitle('Without geom_map'), \n", " plot + geom_map() + ggtitle('With geom_map'), \n", " \n", "])" ] }, { "cell_type": "markdown", "id": "33e0a813-859b-4b32-bd8b-5f91c9bca765", "metadata": {}, "source": [ "#### Reverse Y-axis. The nudge direction is preserved when reversing the y-axis." ] }, { "cell_type": "code", "execution_count": 7, "id": "e0d5ec67-1aa0-4d75-aff6-543661a7cd7a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot = ggplot() + ylim(0, 90) + xlim(-180, 0) + \\\n", " geom_point(x = -90, y = 40, size = 5, color = 'red') + \\\n", " geom_text(x = -90, y = 40, label = 'nudge_y 40 identity', nudge_y = 40) + \\\n", " geom_text(x = -90, y = 40, label = 'nudge_y 40 px', nudge_y = 40, nudge_unit = 'px') + \\\n", " geom_text(x = -90, y = 40, label = 'nudge_y 40 size', nudge_y = 40, nudge_unit = 'size')\n", "\n", "gggrid([\n", " plot + ggtitle('default'), \n", " plot + scale_y_reverse() + ggtitle('y reverse'), \n", "])" ] }, { "cell_type": "markdown", "id": "1298280a-48bd-4395-ae92-3ad4bb1a0856", "metadata": {}, "source": [ "#### Polar coords. identity provides offset in polar coordinates. size and px provide offset in Cartesian coordinates." ] }, { "cell_type": "code", "execution_count": 8, "id": "76e8a5c6-12e4-484d-a7ad-5a3bf9970687", "metadata": {}, "outputs": [], "source": [ "data = {\n", " 'x': [1,2.5,2,5,4],\n", " 'y': [1,2,3,4,5], \n", "}\n", "df = pd.DataFrame(data)" ] }, { "cell_type": "code", "execution_count": 9, "id": "ac823879-9bc0-4cb8-88fb-1daaa89f3f25", "metadata": {}, "outputs": [], "source": [ "identity = ggplot(df) + \\\n", " geom_point(aes(x = 'x', y = 'y'), size = 5, color = 'red') + \\\n", " geom_text(aes(x = 'x', y = 'y', label = 'y') , nudge_y = 0.5)" ] }, { "cell_type": "code", "execution_count": 10, "id": "1c5096db-6ef0-4ccb-863f-57f7f298f9ca", "metadata": {}, "outputs": [], "source": [ "size = ggplot(df) + \\\n", " geom_point(aes(x = 'x', y = 'y'), size = 5, color = 'red') + \\\n", " geom_text(aes(x = 'x', y = 'y', label = 'y'), nudge_y = 10, nudge_unit = 'size')" ] }, { "cell_type": "code", "execution_count": 11, "id": "7b11b939-d419-44da-bc5e-37ec7998086f", "metadata": {}, "outputs": [], "source": [ "px = ggplot(df) + \\\n", " geom_point(aes(x = 'x', y = 'y'), size = 5, color = 'red') + \\\n", " geom_text(aes(x = 'x', y = 'y', label = 'y'), nudge_y = 10, nudge_unit = 'px')" ] }, { "cell_type": "code", "execution_count": 12, "id": "d83e3dd6-d76d-4a8f-849b-6f400dbd7aca", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " identity + coord_polar() + ggtitle('identity'), \n", " size + coord_polar() + ggtitle('size'), \n", " px + coord_polar() + ggtitle('px')\n", "])" ] }, { "cell_type": "code", "execution_count": 13, "id": "9363a77a-16b0-47c7-aae0-72a69a744ca2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " identity + coord_polar(theta = 'y') + ggtitle('identity'), \n", " size + coord_polar(theta = 'y') + ggtitle('size'), \n", " px + coord_polar(theta = 'y') + ggtitle('px')\n", "])" ] }, { "cell_type": "markdown", "id": "75b464e4-129e-4f3a-869c-7ebe0f7f8a0e", "metadata": {}, "source": [ "#### Livemap. Mercator" ] }, { "cell_type": "code", "execution_count": 14, "id": "0a99e214-03f0-4425-9d3e-15f1f5b4a5fc", "metadata": {}, "outputs": [], "source": [ "scale_true = ggplot() + \\\n", " geom_livemap(const_size_zoomin=-1) + \\\n", " geom_point(x = -73, y = 30, size = 5, color = 'red') + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 identity', nudge_y = 50) + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 px', nudge_y = 50, nudge_unit = 'px') + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 size', nudge_y = 50, nudge_unit = 'size')\n", "scale_false = ggplot() + \\\n", " geom_livemap(const_size_zoomin=0) + \\\n", " geom_point(x = -73, y = 30, size = 5, color = 'red') + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 identity', nudge_y = 50) + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 px', nudge_y = 50, nudge_unit = 'px') + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 size', nudge_y = 50, nudge_unit = 'size')\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "d1e2bf35-d34b-4371-a400-b20eee45b810", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " scale_true + ggtitle('scale true'), \n", " scale_false + ggtitle('scale false') \n", "])" ] }, { "cell_type": "markdown", "id": "969975c5-ea8e-40be-b686-39a51a236bfe", "metadata": {}, "source": [ "#### Livemap. Conic. " ] }, { "cell_type": "code", "execution_count": 16, "id": "e6512200-ed80-4ae2-8eb0-1f6717de19f7", "metadata": {}, "outputs": [], "source": [ "scale_true = ggplot() + \\\n", " geom_livemap(const_size_zoomin=-1, projection = 'conic') + \\\n", " geom_point(x = -73, y = 30, size = 5, color = 'red') + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 identity', nudge_y = 50) + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 px', nudge_y = 50, nudge_unit = 'px') + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 size', nudge_y = 50, nudge_unit = 'size')\n", "scale_false = ggplot() + \\\n", " geom_livemap(const_size_zoomin=0, projection = 'conic') + \\\n", " geom_point(x = -73, y = 30, size = 5, color = 'red') + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 identity', nudge_y = 50) + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 px', nudge_y = 50, nudge_unit = 'px') + \\\n", " geom_text(x = -73, y = 30, label = 'nudge_y 50 size', nudge_y = 50, nudge_unit = 'size')" ] }, { "cell_type": "code", "execution_count": 17, "id": "7a9b4c53-ef45-4f66-8bd1-58e22d0c80fe", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " scale_true + ggtitle('scale true'), \n", " scale_false + ggtitle('scale false') \n", "])" ] }, { "cell_type": "code", "execution_count": null, "id": "a64f74fc-9de9-4132-a328-78b0e741dd5f", "metadata": {}, "outputs": [], "source": [] } ], "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 }