{ "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": "45a22b24-da0c-449b-a851-efb9b70de14c", "metadata": {}, "source": [ "# Multiline axis labels" ] }, { "cell_type": "code", "execution_count": 3, "id": "1d6c6d8e-66d6-472c-b817-d55441fd7fd6", "metadata": {}, "outputs": [], "source": [ "data = {\n", " \"x\": [\"Single\", \"2\\nlines\", \"Multiline\\nlabel\", \"Label\\n#4\\n(of 4)\"],\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=[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" ] }, { "cell_type": "code", "execution_count": 6, "id": "b4687e37-7b06-4319-908d-57161734dac4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(generate_all_plots('bottom', 0), ncol=3) " ] }, { "cell_type": "code", "execution_count": 7, "id": "fd5e92cb-c4ad-41d0-a1d5-15976721b19c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(generate_all_plots('bottom', 30), ncol=3) " ] }, { "cell_type": "code", "execution_count": 8, "id": "976db71c-b20c-4d97-9b71-95daaf1a0b9f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(generate_all_plots('bottom', 90), ncol=3) " ] }, { "cell_type": "code", "execution_count": null, "id": "ed90baf0-f727-43ee-88f2-6042247d7bf4", "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 }