{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Rotation of Axis Labels\n", "\n", "The `angle` parameter in `element_text()` function allows to rotate the text. \n", "\n", "Note: currently this only works for axis labels, i.e. for `axis_text, axis_text_x, axis_text_y` parameters in `theme()`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. [Default Axis Labels Layout](#1.-Default-Axis-Labels-Layout) \n", "\n", "2. [Layout Labels for Discrete Axis](#2.-Layout-Labels-for-Discrete-Axis) \n", " \n", "3. [Layout Labels for Continuous Axis](#3.-Layout-Labels-for-Continuous-Axis)\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:46.728142Z", "iopub.status.busy": "2024-04-17T07:30:46.728037Z", "iopub.status.idle": "2024-04-17T07:30:47.045880Z", "shell.execute_reply": "2024-04-17T07:30:47.045557Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:47.047492Z", "iopub.status.busy": "2024-04-17T07:30:47.047362Z", "iopub.status.idle": "2024-04-17T07:30:47.050018Z", "shell.execute_reply": "2024-04-17T07:30:47.049687Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()\n", "LetsPlot.set_theme(theme_light())" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:47.066203Z", "iopub.status.busy": "2024-04-17T07:30:47.066091Z", "iopub.status.idle": "2024-04-17T07:30:47.300688Z", "shell.execute_reply": "2024-04-17T07:30:47.300441Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(437, 29)\n" ] }, { "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0PIDcountystateareapoptotalpopdensitypopwhitepopblackpopamerindian...percollegepercprofpoppovertyknownpercpovertyknownpercbelowpovertypercchildbelowpovertpercadultpovertypercelderlypovertyinmetrocategory
01561ADAMSIllinois0.052660901270.96154063917170298...19.6313924.3558596362896.27477713.15144318.01171711.00977612.4438120AAR
12562ALEXANDERIllinois0.01410626759.0000007054349619...11.2433082.8703151052999.08714532.24427845.82651427.38564725.2289760LHR
23563BONDIllinois0.02214991681.4090911447742935...17.0338194.4885721423594.95697412.06884414.03606110.85209012.6974100AAR
34564BOONEIllinois0.017308061812.1176502934412746...17.2789544.1978003033798.4775697.20901911.1795365.5360136.2170471ALU
45565BROWNIllinois0.0185836324.222222526454714...14.4759993.367680481582.50514013.52024913.02288911.14321119.2000000AAR
\n", "

5 rows × 29 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 PID county state area poptotal popdensity \\\n", "0 1 561 ADAMS Illinois 0.052 66090 1270.961540 \n", "1 2 562 ALEXANDER Illinois 0.014 10626 759.000000 \n", "2 3 563 BOND Illinois 0.022 14991 681.409091 \n", "3 4 564 BOONE Illinois 0.017 30806 1812.117650 \n", "4 5 565 BROWN Illinois 0.018 5836 324.222222 \n", "\n", " popwhite popblack popamerindian ... percollege percprof \\\n", "0 63917 1702 98 ... 19.631392 4.355859 \n", "1 7054 3496 19 ... 11.243308 2.870315 \n", "2 14477 429 35 ... 17.033819 4.488572 \n", "3 29344 127 46 ... 17.278954 4.197800 \n", "4 5264 547 14 ... 14.475999 3.367680 \n", "\n", " poppovertyknown percpovertyknown percbelowpoverty percchildbelowpovert \\\n", "0 63628 96.274777 13.151443 18.011717 \n", "1 10529 99.087145 32.244278 45.826514 \n", "2 14235 94.956974 12.068844 14.036061 \n", "3 30337 98.477569 7.209019 11.179536 \n", "4 4815 82.505140 13.520249 13.022889 \n", "\n", " percadultpoverty percelderlypoverty inmetro category \n", "0 11.009776 12.443812 0 AAR \n", "1 27.385647 25.228976 0 LHR \n", "2 10.852090 12.697410 0 AAR \n", "3 5.536013 6.217047 1 ALU \n", "4 11.143211 19.200000 0 AAR \n", "\n", "[5 rows x 29 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "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", "df.head()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:47.301947Z", "iopub.status.busy": "2024-04-17T07:30:47.301866Z", "iopub.status.idle": "2024-04-17T07:30:47.303418Z", "shell.execute_reply": "2024-04-17T07:30:47.303239Z" } }, "outputs": [], "source": [ "highlight_axis_labels = theme(\n", " text=element_text(color='gray', size=13), \n", " axis_text=element_text(color='blue')\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. Default Axis Labels Layout" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:47.304670Z", "iopub.status.busy": "2024-04-17T07:30:47.304548Z", "iopub.status.idle": "2024-04-17T07:30:47.343009Z", "shell.execute_reply": "2024-04-17T07:30:47.342802Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(df) + geom_jitter(aes(\"state\", \"poptotal\"), color='light_gray')\n", "p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. Layout Labels for Discrete Axis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's change rotation angle to check placement of labels on a discrete axis. Labels on y-axis are removed for this demo." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 2.1. Horizontal Axis" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:47.344416Z", "iopub.status.busy": "2024-04-17T07:30:47.344335Z", "iopub.status.idle": "2024-04-17T07:30:47.361564Z", "shell.execute_reply": "2024-04-17T07:30:47.361231Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_x_both = p + theme(axis_text_y=element_blank()) + scale_x_discrete(position=\"both\")\n", "\n", "gggrid([\n", " p_x_both + theme(axis_text_x=element_text(angle=10)) + highlight_axis_labels + ggtitle(\"10°\"),\n", " p_x_both + theme(axis_text_x=element_text(angle=90)) + highlight_axis_labels + ggtitle(\"90°\")\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 2.2. Vertical Axis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When the rotation angle is set to 90°, some labels are not displayed to avoid overlapping." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:47.362859Z", "iopub.status.busy": "2024-04-17T07:30:47.362667Z", "iopub.status.idle": "2024-04-17T07:30:47.378678Z", "shell.execute_reply": "2024-04-17T07:30:47.378497Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p_x_both + coord_flip() + theme(axis_text_x=element_text(angle=10)) + highlight_axis_labels + ggtitle(\"10°\"),\n", " p_x_both + coord_flip() + theme(axis_text_x=element_text(angle=90)) + highlight_axis_labels + ggtitle(\"90°\")\n", "]) + ggsize(1000, 300)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3. Layout Labels for Continuous Axis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For continuous axis the number of labels varies depending on how many it can fit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 3.1. Horizontal Axis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Changing the angle of rotation changes the number of labels on the axis:\n", "90° rotation allows to place more labels on the horizontal axis." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:47.379881Z", "iopub.status.busy": "2024-04-17T07:30:47.379777Z", "iopub.status.idle": "2024-04-17T07:30:47.397185Z", "shell.execute_reply": "2024-04-17T07:30:47.397005Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_y_both = p + theme(axis_text_x=element_blank()) + scale_y_continuous(position=\"both\")\n", "\n", "gggrid([\n", " p_y_both + coord_flip() + theme(axis_text=element_text(angle=10)) + highlight_axis_labels + ggtitle(\"10°\"),\n", " p_y_both + coord_flip() + theme(axis_text=element_text(angle=90)) + highlight_axis_labels + ggtitle(\"90°\")\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 3.2. Vertical Axis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By changing the angle of rotation, thereby increasing the label heights, we get a reduction in the total number of labels on the vertical axis." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:47.398267Z", "iopub.status.busy": "2024-04-17T07:30:47.398192Z", "iopub.status.idle": "2024-04-17T07:30:47.414506Z", "shell.execute_reply": "2024-04-17T07:30:47.414241Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p_y_both + theme(axis_text=element_text(angle=10)) + highlight_axis_labels + ggtitle(\"10°\"),\n", " p_y_both + theme(axis_text=element_text(angle=90)) + highlight_axis_labels + ggtitle(\"90°\")\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.10.13" } }, "nbformat": 4, "nbformat_minor": 4 }