{ "cells": [ { "cell_type": "markdown", "id": "0be28d50-288c-482c-b96f-ea37cd96b4d0", "metadata": {}, "source": [ "# Axis Minor Ticks\n", "\n", "The `axis_minor_ticks` and `axis_minor_ticks_length` parameters in `theme()` control\n", "the appearance of axis minor ticks, i.e., the tick marks drawn between major ticks.\n", "\n", "* `axis_minor_ticks` - tick line attributes set via `element_line()`\n", "* `axis_minor_ticks_length` - tick length in pixels\n", "* `axis_minor_ticks_x` / `axis_minor_ticks_y`, `axis_minor_ticks_length_x` / `axis_minor_ticks_length_y` -\n", "directional overrides allow controlling only horizontal or vertical axes.\n", "\n", "Minor ticks are placed at positions computed by the scale (e.g., midpoints between major breaks\n", "for continuous scales). They do not carry labels as for now." ] }, { "cell_type": "code", "execution_count": 1, "id": "08f8fca6-56e4-4212-8d02-c0f3ba2b34df", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "import numpy as np\n", "import pandas as pd\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "b0cfb1bf-9865-4e60-9fc9-3284be33ca16", "metadata": {}, "source": [ "## General Usage\n", "Minor ticks can improve readability of continuous axes, especially when major breaks are sparse." ] }, { "cell_type": "code", "execution_count": 2, "id": "6a1c07f6-58fb-4079-80e9-37b02ed62651", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Generate sample data from a normal distribution\n", "np.random.seed(42)\n", "x = np.random.normal(loc=0, scale=1, size=2000)\n", "df_hist = {'x': x}\n", "\n", "plot_norm = ggplot(df_hist) + \\\n", " geom_histogram(aes(x='x', y='..density..'), bins=40, color='white', fill='light_sky_blue', alpha=0.7) + \\\n", " geom_density(aes(x='x'), color='tomato', size=1.2, alpha=0) + \\\n", " scale_x_continuous(breaks=[-3, -2, -1, 0, 1, 2, 3]) + \\\n", " labs(title=\"Normal Distribution\",\n", " x=\"Value\",\n", " y=\"Count / Density\")\n", "plot_norm" ] }, { "cell_type": "code", "execution_count": 3, "id": "a9481ce0-61bd-42e2-818f-f30bd5820ef5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_norm + \\\n", " ggtitle(\"Normal Distribution with Minor Ticks\") + \\\n", " theme(\n", " axis_ticks_length=8,\n", " \n", " axis_minor_ticks=element_line(color='gray85'),\n", " axis_minor_ticks_length=5,\n", " \n", " axis_minor_ticks_x=element_line(color='gray66', size=2),\n", " ) " ] }, { "cell_type": "markdown", "id": "011bc188-f818-4757-bc6c-248514b7fcd6", "metadata": {}, "source": [ "## Minor Ticks as Separators\n", "\n", "Minor ticks can be styled as vertical separators, helping distinguish categories more clearly." ] }, { "cell_type": "code", "execution_count": 4, "id": "e1eb357d-a4ae-4f67-9e59-c66b8ca43ff7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(437, 29)\n" ] } ], "source": [ "df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/midwest.csv')\n", "df[\"state\"] = df[\"state\"].map({\n", " \"IL\": \"Illinois\",\n", " \"IN\":\" Indiana\",\n", " \"MI\": \"Michigan\",\n", " \"OH\": \"Ohio\",\n", " \"WI\": \"Wisconsin\",\n", "})\n", "print(df.shape)\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "1b1a74ed-267e-4738-be6c-9cb263af3e57", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base_plot = ggplot(df) + \\\n", " geom_jitter(aes(\"state\", \"poptotal\"), seed=42, alpha=0.3) + \\\n", " ggtitle(\"Population Distribution by State\") + \\\n", " theme_gray()\n", "base_plot" ] }, { "cell_type": "code", "execution_count": 6, "id": "0cb27a15-6007-4117-b5d6-334ec0cc619a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base_plot + \\\n", " ggtitle(\"Minor ticks as separators\") + \\\n", " theme(\n", " # disable major ticks\n", " axis_ticks_x=element_blank(),\n", " \n", " # and add enlarged minor ticks\n", " axis_minor_ticks_length_x=15.0,\n", "\n", " axis_minor_ticks_x=element_line(\n", " color=\"grey\"\n", " )\n", " )" ] } ], "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.23" } }, "nbformat": 4, "nbformat_minor": 5 }