{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Rotated tick labels\n\nRotating tick labels can be useful when the labels are long and overlap with each other.\nAdjust the tick properties using `~.Axes.tick_params`: Set the angle in degrees via\nthe *rotation* parameter. Set the *rotation_mode* parameter to \"xtick\" / \"ytick\" to\nmake the text point towards the tick, see also `~.Text.set_rotation_mode`.\n\nNote: We use ``layout=\"constrained\"`` to make sure there is enough space for the tick\nlabels so that they are not cut off.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\npopulation = {\n 'India': 1417.492,\n 'China': 1404.89,\n 'United States': 341.784857,\n 'Indonesia': 284.438782,\n 'Pakistan': 241.499431,\n 'Nigeria': 223.8,\n 'Brazil': 213.421037,\n 'Bangladesh': 169.828911,\n 'Russia': 146.028325,\n 'Mexico': 131.001723,\n 'Japan': 122.95,\n 'Philippines': 114.1236,\n 'DR Congo': 112.832,\n 'Ethiopia': 111.652998,\n 'Egypt': 107.868296,\n 'Vietnam': 102.3,\n}\n\nfig, ax = plt.subplots(layout=\"constrained\")\nax.bar(population.keys(), population.values())\nax.tick_params(\"x\", rotation=45, rotation_mode=\"xtick\")\nax.set_ylabel(\"population (millions)\")\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.tick_params` / `matplotlib.pyplot.tick_params`\n\n.. tags:: component: ticks, plot-type: bar\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.12.15" } }, "nbformat": 4, "nbformat_minor": 0 }