{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "96d0b1fa-5b18-47e3-b40f-b36d95f52121", "metadata": { "execution": { "iopub.execute_input": "2024-12-17T13:18:09.459499Z", "iopub.status.busy": "2024-12-17T13:18:09.459406Z", "iopub.status.idle": "2024-12-17T13:18:09.805186Z", "shell.execute_reply": "2024-12-17T13:18:09.804772Z" } }, "outputs": [], "source": [ "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "fda258d8-2f71-4cb0-95ff-431553bc6e3b", "metadata": { "execution": { "iopub.execute_input": "2024-12-17T13:18:09.806579Z", "iopub.status.busy": "2024-12-17T13:18:09.806465Z", "iopub.status.idle": "2024-12-17T13:18:09.808726Z", "shell.execute_reply": "2024-12-17T13:18:09.808485Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "63a4a3f2-f83e-4576-b7c2-464ab0c6dfed", "metadata": { "execution": { "iopub.execute_input": "2024-12-17T13:18:09.809622Z", "iopub.status.busy": "2024-12-17T13:18:09.809550Z", "iopub.status.idle": "2024-12-17T13:18:09.811106Z", "shell.execute_reply": "2024-12-17T13:18:09.810867Z" } }, "outputs": [], "source": [ "color_margin = \"#b3cde3\"\n", "color_key = \"#decbe4\"\n", "color_label = \"#fddaec\"\n", "color_plot = \"#ccebc5\"" ] }, { "cell_type": "code", "execution_count": 4, "id": "eac64a33-90fe-4a9a-8204-24ad926c0906", "metadata": { "execution": { "iopub.execute_input": "2024-12-17T13:18:09.812059Z", "iopub.status.busy": "2024-12-17T13:18:09.811990Z", "iopub.status.idle": "2024-12-17T13:18:09.816425Z", "shell.execute_reply": "2024-12-17T13:18:09.816181Z" } }, "outputs": [], "source": [ "def legend_element(elem_bnd):\n", " return geom_rect(xmin=elem_bnd[0], xmax=elem_bnd[2],\n", " ymin=elem_bnd[1], ymax=elem_bnd[3],\n", " fill=color_label, color=color_label, size=0) + \\\n", " geom_rect(xmin=elem_bnd[0], xmax=elem_bnd[0] + 1.5,\n", " ymin=elem_bnd[1], ymax=elem_bnd[3],\n", " fill=color_key, color=color_key, size=0) + \\\n", " geom_text(x=elem_bnd[0] + 0.1, y=(elem_bnd[3] + elem_bnd[1]) / 2,\n", " label=\"key\", vjust=0.5, hjust=0, size=10) + \\\n", " geom_text(x=elem_bnd[0] + 1.6, y=(elem_bnd[3] + elem_bnd[1]) / 2,\n", " label=\"label\", vjust=0.5, hjust=0, size=10)\n", "\n", "def legend(x, y, w=10, h=8):\n", " xmin = x\n", " xmax = x + w\n", " ymin = y\n", " ymax = ymin + h\n", "\n", " margin = 1\n", " space = 1\n", " title_h = 1.5\n", " elem_w = ((w - margin * 2) - space) / 2\n", " elem_h = ((h - margin * 2)) / 2 - space - 0.2\n", "\n", " # xmin, ymin, xmax, ymax\n", " inner_bnd = [xmin + margin, ymin + margin,\n", " xmax - margin, ymax - margin]\n", " elem11 = [inner_bnd[0], inner_bnd[3] - title_h - elem_h,\n", " inner_bnd[0] + elem_w, inner_bnd[3] - title_h]\n", " elem12 = [inner_bnd[0] + elem_w + space, inner_bnd[3] - title_h - elem_h,\n", " inner_bnd[2], inner_bnd[3] - title_h]\n", " elem21 = [elem11[0], inner_bnd[1], elem11[2], inner_bnd[1] + elem_h]\n", " elem22 = [elem12[0], inner_bnd[1], inner_bnd[2], elem21[3]]\n", "\n", " # plot elements\n", " legend_background = geom_rect(xmin=xmin, xmax=xmax,\n", " ymin=ymin, ymax=ymax,\n", " fill=color_margin, size=1.5)\n", " legend_elements_box = geom_rect(xmin=inner_bnd[0], xmax=inner_bnd[2],\n", " ymin=inner_bnd[1], ymax=inner_bnd[3],\n", " fill='white', size=0)\n", " title = geom_text(label=\"Title\",\n", " x=inner_bnd[0] + 0.1, y=inner_bnd[3] - title_h / 2,\n", " vjust=0.5, hjust=0, size=10)\n", " row1 = legend_element(elem11) + legend_element(elem12)\n", " row2 = legend_element(elem21) + legend_element(elem22)\n", " \n", " return legend_background + legend_elements_box + title + row1 + row2\n", "\n", "def annotation_line(x, y, xend=None, yend=None, type_arrow='both', linetype='solid'):\n", " xend = xend if xend is not None else x\n", " yend = yend if yend is not None else y\n", " with_arrow = None\n", " if type_arrow is not None:\n", " with_arrow = arrow(length=5, ends=type_arrow, type='closed')\n", "\n", " return geom_segment(x=x, y=y, xend=xend, yend=yend, linetype=linetype, arrow=with_arrow)" ] }, { "cell_type": "code", "execution_count": 5, "id": "089b2bc1-d219-493e-bcf7-7d15ad23d4d8", "metadata": { "execution": { "iopub.execute_input": "2024-12-17T13:18:09.817299Z", "iopub.status.busy": "2024-12-17T13:18:09.817232Z", "iopub.status.idle": "2024-12-17T13:18:09.845991Z", "shell.execute_reply": "2024-12-17T13:18:09.845721Z" } }, "outputs": [], "source": [ "plotting_area = geom_rect(xmin=0, xmax=3, ymin=-1, ymax=19,\n", " fill=color_plot, color=color_margin,\n", " size=14, alpha=0.5) + \\\n", " geom_text(label='plotting area', x=2, y=10,\n", " hjust=0.5, vjust=0, angle=90, size=11, color='grey') + \\\n", " geom_text(label='plot_inset', x=2.8, y=10,\n", " hjust=0.5, vjust=1, angle=90, family='Courier')\n", "legend_box_spacing = geom_text(label='legend_box_spacing', x=4.2, y=10.5,\n", " hjust=0, vjust=0, angle=90, family='Courier') + \\\n", " annotation_line(x=3.3, y=10.2, xend=5)\n", "legend_boxes = legend(x=5, y=10, w=15, h=8.5) + \\\n", " legend(x=5, y=0, w=12, h=8.5) + \\\n", " geom_text(label='legend_justification=[0,1]', x=23, y=19,\n", " hjust=1, vjust=0, family='Courier') + \\\n", " annotation_line(x=-1, y=18.5, xend=22, type_arrow=None, linetype='dashed')\n", "legend_annotations = (geom_text(label='legend_margin', x=15, y=18, hjust=0, family='Courier')) + \\\n", " (geom_text(label='legend_spacing_y', x=6, y=9.2, hjust=0, family='Courier')\n", " + annotation_line(x=5.4, y=8.5, yend=10)) + \\\n", " (geom_text(label='legend_key_spacing_x', x=12.8, y=9.2, hjust=0, family='Courier')\n", " + annotation_line(x=12, y=11.5, xend=13)\n", " + annotation_line(x=12.5, y=9.2, yend=11.5, type_arrow=None)) + \\\n", " (geom_text(label='legend_key_spacing_y', x=14, y=13.5, hjust=0, family='Courier')\n", " + annotation_line(x=13.8, y=13.1, yend=13.9)) + \\\n", " (geom_text(label='legend_box_just=\\'left\\' (legend_box=\\'vertical\\')',\n", " x=5.3, y=-1, hjust=0, family='Courier')\n", " + annotation_line(x=5, y=-2, yend=20, type_arrow=None, linetype='dashed')) + \\\n", " (geom_text(label='legend_key_width', x=12, y=7, hjust=0, family='Courier')\n", " + annotation_line(x=11.5, y=6.5, xend=13)) + \\\n", " (geom_text(label='legend_key_height', x=10.5, y=3.5, hjust=0, family='Courier')\n", " + annotation_line(x=11.2, y=4, yend=5.9))" ] }, { "cell_type": "code", "execution_count": 6, "id": "ebef8ee8-9cc6-4c4b-9529-a71fc849cd3d", "metadata": { "execution": { "iopub.execute_input": "2024-12-17T13:18:09.847126Z", "iopub.status.busy": "2024-12-17T13:18:09.847052Z", "iopub.status.idle": "2024-12-17T13:18:09.860630Z", "shell.execute_reply": "2024-12-17T13:18:09.860384Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " plotting_area + \\\n", " legend_box_spacing + \\\n", " legend_boxes + \\\n", " legend_annotations + \\\n", " coord_cartesian(xlim=(1.2, 23)) + \\\n", " theme_void() + \\\n", " ggsize(1000, 800)" ] } ], "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": 5 }