{ "cells": [ { "cell_type": "markdown", "id": "ddd7bbc3-b7c1-4b3b-8ac0-24c37e8c6cc5", "metadata": {}, "source": [ "# Multiline Axis Labels\n", "\n", "Multiline axis labels allow better visualization of long categorical values. Instead of overcrowding the axis, labels can now be formatted across multiple lines, enhancing readability. \n", "\n", "* To create a multiline axis label, simply add \"\\n\" inside the string where you want the line break." ] }, { "cell_type": "code", "execution_count": 1, "id": "e83da232-c578-4c48-848e-53fb1199a51c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "import pandas as pd\n", "import numpy as np\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "63f9d1ac-de82-4cda-85f1-3f3d8998eef0", "metadata": {}, "source": [ "## Quarters & Years" ] }, { "cell_type": "code", "execution_count": 2, "id": "8ea6aa2f-e6b9-42d1-acb0-8984bd93d2d6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Quarter_YearTypeAmount
0Q1-2022Income11564
1Q1-2022Expenses9924
2Q2-2022Income11365
3Q2-2022Expenses10432
\n", "
" ], "text/plain": [ " Quarter_Year Type Amount\n", "0 Q1-2022 Income 11564\n", "1 Q1-2022 Expenses 9924\n", "2 Q2-2022 Income 11365\n", "3 Q2-2022 Expenses 10432" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a DataFrame with artificial income and expenses\n", "np.random.seed(42)\n", "years = [2022, 2023, 2024]\n", "quarters = [\"Q1\", \"Q2\", \"Q3\", \"Q4\"]\n", "\n", "data = []\n", "for year in years:\n", " base_income = np.random.randint(5000, 15000) # Base income\n", " base_expenses = base_income * np.random.uniform(0.6, 0.9) # Expenses as 60-90% of income\n", " \n", " for quarter in quarters:\n", " income = base_income + np.random.randint(-2000, 2000) # Add variation\n", " expenses = base_expenses + np.random.randint(-1500, 1500)\n", " \n", " quarter_year = f\"{quarter}-{year}\"\n", " data.append({\"Quarter_Year\": quarter_year, \"Type\": \"Income\", \"Amount\": round(income)})\n", " data.append({\"Quarter_Year\": quarter_year, \"Type\": \"Expenses\", \"Amount\": round(expenses)})\n", "\n", "df = pd.DataFrame(data)\n", "df.head(4)" ] }, { "cell_type": "code", "execution_count": 3, "id": "18870e14-f8f7-4bac-a914-ca9c188b93a2", "metadata": {}, "outputs": [], "source": [ "# Multiline labels for X-axis\n", "x_labels_mapping = [f\"{q}\\n{year}\" if q == \"Q1\" else q for year in years for q in quarters]" ] }, { "cell_type": "code", "execution_count": 4, "id": "9a735efd-24e3-450b-890d-f0eb076e17a5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"Quarter_Year\", y=\"Amount\", color=\"Type\")) + \\\n", " geom_line(size=1.5) + \\\n", " geom_point(size=4) + \\\n", " scale_x_discrete(labels=x_labels_mapping) + \\\n", " labs(title=\"Quarterly Income vs Expenses\",\n", " x=\"Quarters\",\n", " y=\"Amount ($)\") + \\\n", " theme(axis_text_x=element_text(angle=30))" ] } ], "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.21" } }, "nbformat": 4, "nbformat_minor": 5 }