{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "defacae4-2a0b-4a9f-8b57-01bb469350c5", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from datetime import datetime\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "d33a1c37-0dfe-4a4f-8fd2-76eff43e35eb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "4d95fba1-3ea1-4ce8-a70e-40060a8bb3c2", "metadata": {}, "source": [ "# Verifying hjust and vjust for axis labels" ] }, { "cell_type": "code", "execution_count": 3, "id": "df4ed66a-8586-4828-97e3-ea2714ffa25f", "metadata": {}, "outputs": [], "source": [ "data = {\n", " \"x\": [\"OXXXXXXX\", \"OOOOO\", \"It's lXXX\", \"XOOOOXXX\"],\n", " \"y\": [500, 1000, 500, 0],\n", " \"z\": [\"a\", \"b\", \"c\", \"d\"]\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "id": "46a9ea97-abcf-4cb9-9553-f7732dcbf686", "metadata": {}, "outputs": [], "source": [ "justifications = [1.0, 0.5, 0.0]\n", "angles=[90, 30, 0, -30, 90]" ] }, { "cell_type": "code", "execution_count": 5, "id": "776fd525-2402-480d-889e-40908bd6ee14", "metadata": {}, "outputs": [], "source": [ "def create_plot(position, angle, hjust, vjust):\n", " if position == 'top' or position == 'bottom':\n", " theme_element = theme(axis_text_x=element_text(angle=angle, hjust=hjust, vjust=vjust), axis_title=\"blank\") + \\\n", " scale_x_discrete(position = f'{position}')\n", " x_data = \"x\"\n", " else:\n", " theme_element = theme(axis_text_y=element_text(angle=angle, hjust=hjust, vjust=vjust), axis_title=\"blank\") + \\\n", " scale_y_discrete(position = f'{position}') \n", " x_data = \"z\"\n", " return (\n", " ggplot(data) + \n", " geom_point(aes(x=x_data, y=\"y\"), size=5) + \n", " theme_classic() + \n", " ggtitle(f\"h{hjust}, v{vjust}\") +\n", " theme_element\n", " )\n", "\n", " \n", "def generate_all_plots(position, angle):\n", " return [create_plot(position=position, angle=angle, hjust=hjust, vjust=vjust)\n", " for hjust in justifications\n", " for vjust in justifications]\n", "\n", "\n", "def create_plot_grid(position):\n", " for angle in angles:\n", " print('-'*150)\n", " print(f'ANGLE: {angle}°')\n", " (gggrid(generate_all_plots(position, angle), ncol=3) + ggsize(1024, 1024)).show()" ] }, { "cell_type": "markdown", "id": "6012def9-7f8c-4c20-9c5b-916172c1eed3", "metadata": {}, "source": [ "## Default values" ] }, { "cell_type": "code", "execution_count": 6, "id": "762cd971-9898-40e2-a8ef-f9ac0b543b3a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data) + \\\n", " geom_point(aes(x=\"x\", y=\"y\"), size=5) + \\\n", " theme_classic() + \\\n", " theme(axis_text_x=element_text(angle=30), axis_text_y=element_text(angle=30)) + \\\n", " ggsize(450, 312)" ] }, { "cell_type": "markdown", "id": "f2a99504-ad97-4b23-9c6b-a35fb7fefc79", "metadata": {}, "source": [ "# Bottom axis" ] }, { "cell_type": "code", "execution_count": 7, "id": "ee942896-73b8-4af9-af78-ee0c5345a292", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 90°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 30°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 0°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: -30°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 90°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "create_plot_grid('bottom')" ] }, { "cell_type": "markdown", "id": "5a1477b8-6db2-4c04-bcc4-0a2fbec921a8", "metadata": {}, "source": [ "# Top axis" ] }, { "cell_type": "code", "execution_count": 8, "id": "6855f2b9-2ce5-4bac-b75c-c09186c1c588", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 90°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 30°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 0°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: -30°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 90°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "create_plot_grid('top')" ] }, { "cell_type": "markdown", "id": "e7d2b331-a9d4-4406-a8a5-aee87c44a796", "metadata": {}, "source": [ "# Left axis" ] }, { "cell_type": "code", "execution_count": 9, "id": "042a066f-59fb-4044-929e-830652645555", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 90°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 30°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 0°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: -30°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 90°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "create_plot_grid('left')" ] }, { "cell_type": "markdown", "id": "04aafc36-7e86-4195-96e4-69959a5bd84f", "metadata": {}, "source": [ "# Right axis" ] }, { "cell_type": "code", "execution_count": 10, "id": "7e55153c-816f-45af-ae27-db38a88b2dc5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 90°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 30°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 0°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: -30°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------------------------------------------------------------------------------------------\n", "ANGLE: 90°\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "create_plot_grid('right')" ] }, { "cell_type": "markdown", "id": "dc0c7cc0-0791-4364-b180-ff5ee1eecaa0", "metadata": {}, "source": [ "# Unusual values" ] }, { "cell_type": "code", "execution_count": 11, "id": "35dca21e-9b3c-4872-bad9-67bf9af1fdcd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data) + \\\n", " geom_point(aes(x=\"x\", y=\"y\"), size=5) + \\\n", " theme_classic() + \\\n", " theme(axis_text_x=element_text(angle=-30, hjust = 0.7, vjust=0.2), axis_text_y=element_text(angle=30, hjust=1, vjust=0.2)) + \\\n", " ggsize(450, 312)" ] }, { "cell_type": "code", "execution_count": null, "id": "976db71c-b20c-4d97-9b71-95daaf1a0b9f", "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.8.18" } }, "nbformat": 4, "nbformat_minor": 5 }