{ "cells": [ { "cell_type": "markdown", "id": "a62d5ff5-858f-43c3-9a76-6b3ed8b5b957", "metadata": {}, "source": [ "# Parameter `size_unit` in Geoms `geom_point()`, `geom_text()` and `geom_label()`\n", "\n", "The `size_unit` parameter relates the size of a geometry to the length of the unit step along one of the axes." ] }, { "cell_type": "code", "execution_count": 1, "id": "6afaa8ff-8714-4575-9458-e3ee4434d1ad", "metadata": {}, "outputs": [], "source": [ "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "3d059f1c-245f-4690-b6bb-69137c4f3150", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "b5358334-847f-4f63-8e12-4e5f90ca7403", "metadata": {}, "outputs": [], "source": [ "size = 10\n", "data = {\n", " 'x': [0, 0, 0],\n", " 'y': [-size, 0, size],\n", " 'text': ['A', 'b', '0'],\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "id": "ac3bf8eb-5408-4c3c-818c-375e54a552ec", "metadata": {}, "outputs": [], "source": [ "def title(geom_name, size_unit=None):\n", " if size_unit is None:\n", " size_unit_text = \"size_unit - default\"\n", " else:\n", " size_unit_text = \"size_unit='{0}'\".format(size_unit)\n", " return ggtitle(\"{geom}: size={size}, {size_unit_text}\".format(\n", " geom=geom_name,\n", " size=size,\n", " size_unit_text=size_unit_text\n", " ))" ] }, { "cell_type": "code", "execution_count": 5, "id": "f347dd10-0311-4528-8c7c-a6259b5dca67", "metadata": {}, "outputs": [], "source": [ "p = ggplot(data, aes('x', 'y')) + scale_y_continuous(limits=[-size, size], expand=[.15, 0])" ] }, { "cell_type": "code", "execution_count": 6, "id": "4c561e36-6267-4c09-a095-642db5d51605", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_point(size=size) + title(\"Point\"),\n", " p + geom_point(size=size, size_unit='y') + title(\"Point\", 'y'),\n", "])" ] }, { "cell_type": "code", "execution_count": 7, "id": "ce026b28-bdb0-4fd0-85f8-037bca968e7f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_text(aes(label='text'), size=size) + title(\"Text\"),\n", " p + geom_text(aes(label='text'), size=size, size_unit='y') + title(\"Text\", 'y'),\n", "])" ] }, { "cell_type": "code", "execution_count": 8, "id": "d68d2e13-3b0e-42fe-aa92-2d29a5607c1f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_label(aes(label='text'), size=size) + title(\"Label\"),\n", " p + geom_label(aes(label='text'), size=size, size_unit='y') + title(\"Label\", 'y'),\n", "])" ] }, { "cell_type": "markdown", "id": "87e71dca-e6ab-42ae-a779-2ba189040bee", "metadata": {}, "source": [ "#### Use Case: Drawing a Venn Diagram" ] }, { "cell_type": "code", "execution_count": 9, "id": "959ef24a-18f5-4ff4-85df-3cc781ee6762", "metadata": {}, "outputs": [], "source": [ "from math import sqrt" ] }, { "cell_type": "code", "execution_count": 10, "id": "3ab025fb-c42e-4853-9b85-ac4fb8aab6ea", "metadata": {}, "outputs": [], "source": [ "r = .5\n", "h = r * sqrt(3)/2\n", "sets_data = {\n", " 'x': [0, r, r/2],\n", " 'y': [0, 0, h],\n", " 'size': [2*r, 2*r, 2*r],\n", " 'color': ['#e24462', '#306998', '#ffe873'],\n", "}\n", "texts_data = {\n", " 'x': [-r/2, r + r/2, r/2, r/2, -r/8, r + r/8, r/2],\n", " 'y': [-r/4, -r/4, h + r/2, -3*r/8, 5*r/8, 5*r/8, 5*r/16],\n", " 'text': [\"Kotlin\", \"Geospatial\\nVisualization\", \"Grammar\\nof\\nGraphics\", \"ArcGIS\", \"Kandy\", \"ggmap\", \"Lets-Plot\"],\n", " 'size': [.4*r]*7,\n", "}" ] }, { "cell_type": "code", "execution_count": 11, "id": "c7b0041e-f09e-43d3-bb1f-be9f54906116", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_point(aes('x', 'y', size='size', fill='color'), \\\n", " data=sets_data, size_unit='x', \\\n", " shape=21, alpha=.5, color='black', \\\n", " tooltips='none') + \\\n", " geom_text(aes('x', 'y', label='text', size='size'), \\\n", " data=texts_data, size_unit='x') + \\\n", " scale_size_identity() + scale_fill_identity() + \\\n", " xlim(-r, 2*r) + ylim(-r, h + r) + \\\n", " coord_fixed() + \\\n", " ggsize(600, 600) + \\\n", " theme_void()" ] } ], "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.8.19" } }, "nbformat": 4, "nbformat_minor": 5 }