{ "cells": [ { "cell_type": "markdown", "id": "ddd7bbc3-b7c1-4b3b-8ac0-24c37e8c6cc5", "metadata": {}, "source": [ "# Axis Label Justification (`hjust`, `vjust`)\n", "\n", "The `hjust` and `vjust` parameters control the horizontal and vertical justification of axis labels in lets-plot.\n", "\n", "- **`hjust` (Horizontal Justification):** \n", " Adjusts the horizontal alignment of the label relative to its default position. \n", " - `hjust = 0.0` aligns the label to the left. \n", " - `hjust = 0.5` centers the label. \n", " - `hjust = 1.0` aligns the label to the right. \n", "\n", "\n", "- **`vjust` (Vertical Justification):** \n", " Adjusts the vertical alignment of the label relative to its default position. \n", " - `vjust = 0.0` aligns the label to the bottom. \n", " - `vjust = 0.5` centers the label vertically. \n", " - `vjust = 1.0` aligns the label to the top.\n", "\n", "Both parameters accept values between 0.0 and 1.0 for precise positioning, allowing for fine-tuned control over label placement in your plots.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "c56e4f8d-96d0-4d9b-b7a3-d09b1f4ffa43", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "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", " \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", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from lets_plot import *\n", "import pandas as pd\n", "\n", "LetsPlot.setup_html()\n", "\n", "# Load the mpg dataset\n", "mpg = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "mpg.head(3)" ] }, { "cell_type": "code", "execution_count": 2, "id": "49a56790-fce8-4a32-b742-5290cbff55ca", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Base plot with manufacturer on x-axis\n", "p = ggplot(mpg, aes(x=as_discrete('manufacturer'), y='hwy')) + \\\n", " geom_boxplot(fill='#7CF', color='#888') + \\\n", " ggtitle(\"Manufacturer vs Highway MPG\") + \\\n", " xlab(\"Manufacturer\") + ylab(\"Highway MPG\")\n", "\n", "p + ggtitle(\"Default Tick Labels\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "8325c742-c0c6-497c-865b-44f8c0ff301b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + ggtitle(\"90° Rotation, Center-Aligned Vertically\") + \\\n", " theme(axis_text_x=element_text(angle=90, vjust=0.5))" ] }, { "cell_type": "code", "execution_count": 4, "id": "0829a514-d55b-4844-8d1e-cd7cfdaee9e2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + ggtitle(\"30° Rotation, Aligned to the Ticks\") + \\\n", " theme(axis_text_x=element_text(angle=30, hjust=1.0, vjust=1.0))" ] }, { "cell_type": "code", "execution_count": 5, "id": "0fe934fe-e69a-4dce-b6a8-b6882e1fdce3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + ggtitle(\"40° Rotation, at the Bottom, Center-aligned Horizontally\") + \\\n", " theme(axis_text_x=element_text(angle=40, hjust=0.5, vjust=0))" ] }, { "cell_type": "code", "execution_count": 6, "id": "5d57293f-ab77-4ba1-8edf-2cb9279c9c4f", "metadata": {}, "outputs": [], "source": [ "cdata = pd.DataFrame({\n", " 'category': [\n", " 'Highly Recommended',\n", " 'Moderately Satisfactory',\n", " 'Needs Improvement',\n", " 'Exceeds Expectations',\n", " 'Barely Acceptable Quality'\n", " ],\n", " 'value': [25, 40, 15, 35, 10]\n", "})\n", "\n", "cp = ggplot(cdata, aes(x='value', y='category')) + \\\n", " geom_bar(stat='identity', fill='#FB7') + \\\n", " ggtitle(\"Y-Axis Label Justification Demo\") + \\\n", " xlab(\"Value\") + ylab(\"Category\")" ] }, { "cell_type": "markdown", "id": "86645032-afe7-4bb2-98eb-98e3bb8b4552", "metadata": {}, "source": [ "* Left-aligning long labels creates a consistent starting edge, improving readability, while aligning with natural left-to-right reading patterns." ] }, { "cell_type": "code", "execution_count": 7, "id": "2f11dbaa-8aea-4dc2-bf45-c8f0bd509f32", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cp + theme(axis_text_y=element_text(angle=0, hjust=1))" ] } ], "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 }