{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Rotation of axis labels\n", "\n", "The `angle` parameter in `element_text()` function allows to rotate the text. This functionality is currently available to rotate axis labels (i.e. for the `axis_text, axis_text_x, axis_text_y` parameters in a `theme()`)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. [Default axis labels layout](#1.-Default-Axis-Labels-Layout) \n", "\n", "2. [Layout labels for the discrete axis](#2.-Layout-labels-for-the-discrete-axis) \n", " \n", "3. [Layout labels for continuous axis](#3.-Layout-labels-for-continuous-axis)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "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": {}, "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", " \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
01561ADAMSIL0.052660901270.96154063917170298...19.6313924.3558596362896.27477713.15144318.01171711.00977612.4438120AAR
12562ALEXANDERIL0.01410626759.0000007054349619...11.2433082.8703151052999.08714532.24427845.82651427.38564725.2289760LHR
23563BONDIL0.02214991681.4090911447742935...17.0338194.4885721423594.95697412.06884414.03606110.85209012.6974100AAR
\n", "

3 rows × 29 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 PID county state area poptotal popdensity popwhite \\\n", "0 1 561 ADAMS IL 0.052 66090 1270.961540 63917 \n", "1 2 562 ALEXANDER IL 0.014 10626 759.000000 7054 \n", "2 3 563 BOND IL 0.022 14991 681.409091 14477 \n", "\n", " popblack popamerindian ... percollege percprof poppovertyknown \\\n", "0 1702 98 ... 19.631392 4.355859 63628 \n", "1 3496 19 ... 11.243308 2.870315 10529 \n", "2 429 35 ... 17.033819 4.488572 14235 \n", "\n", " percpovertyknown percbelowpoverty percchildbelowpovert percadultpoverty \\\n", "0 96.274777 13.151443 18.011717 11.009776 \n", "1 99.087145 32.244278 45.826514 27.385647 \n", "2 94.956974 12.068844 14.036061 10.852090 \n", "\n", " percelderlypoverty inmetro category \n", "0 12.443812 0 AAR \n", "1 25.228976 0 LHR \n", "2 12.697410 0 AAR \n", "\n", "[3 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.head(3)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['IL', 'IN', 'MI', 'OH', 'WI'], dtype=object)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['state'].unique()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "df['state'] = df['state'].map(\n", " {'IL': 'Illinois', 'IN':'Indiana', 'MI':'Michigan', 'OH': 'Ohio', 'WI' : 'Wisconsin'}\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def plot_matrix(plots=[], width=400, height=300, columns=2):\n", " bunch = GGBunch()\n", " for i in range(len(plots)):\n", " row = int(i / columns)\n", " column = i % columns\n", " bunch.add_plot(plots[i], column * width, row * height, width, height)\n", " return bunch.show()\n", "\n", "\n", "angles = [0, 45, 90, -10]\n", "\n", "highlight_axis_labels = theme(\n", " text=element_text(color='gray', size=13), \n", " axis_text=element_text(color='blue')\n", ")\n", " \n", "def plot_with_text_angles(base_plot, text_elem_name):\n", " plots = [] \n", " for angle in angles:\n", " plots.append(\n", " base_plot + ggtitle(\"angle={0}\".format(angle)) + theme(**{text_elem_name: element_text(angle=angle)})\n", " )\n", " return plot_matrix(plots)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. Default Axis Labels Layout" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(df) + geom_jitter(aes('state', 'poptotal'), color='light_gray', seed=42) + scale_y_log10()\n", "p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. Layout labels for the discrete axis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's change rotation angle to check placement of labels on a discrete axis. Labels for the continuous will be disabled to avoid cluttering the plot." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Horizontal axis" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plot_with_text_angles(\n", " p + theme(axis_text_y='blank') + highlight_axis_labels, \n", " 'axis_text_x'\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Change axis position\n", "\n", "plot_with_text_angles(\n", " p + theme(axis_text_y='blank') + highlight_axis_labels + scale_x_discrete(position=\"top\"), \n", " 'axis_text_x'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plot_with_text_angles(\n", " p + theme(axis_text_y='blank') + highlight_axis_labels + coord_flip(),\n", " 'axis_text_x'\n", ")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Change axis position\n", "\n", "plot_with_text_angles(\n", " p + theme(axis_text_y='blank') + highlight_axis_labels + scale_x_discrete(position=\"top\") + coord_flip(),\n", " 'axis_text_x'\n", ")" ] }, { "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": [ "##### 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": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plot_with_text_angles(\n", " p + theme(axis_text_x='blank') + highlight_axis_labels + coord_flip(),\n", " 'axis_text_y'\n", ")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Change axis position\n", "\n", "plot_with_text_angles(\n", " p + theme(axis_text_x='blank') + highlight_axis_labels + coord_flip() + scale_y_log10(position='right'),\n", " 'axis_text_y'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 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": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plot_with_text_angles(\n", " p + theme(axis_text_x='blank') + highlight_axis_labels,\n", " 'axis_text_y'\n", ")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Change axis position\n", "\n", "plot_with_text_angles(\n", " p + theme(axis_text_x='blank') + highlight_axis_labels + scale_y_log10(position='right'), \n", " 'axis_text_y'\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.8.18" } }, "nbformat": 4, "nbformat_minor": 4 }