{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Align tick labels\n\nBy default, tick labels are aligned towards the axis. This means the set of\n*y* tick labels appear right-aligned. Because the alignment reference point\nis on the axis, left-aligned tick labels would overlap the plotting area.\nTo achieve a good-looking left-alignment, you have to additionally increase\nthe padding.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\npopulation = {\n \"Sydney\": 5.2,\n \"Mexico City\": 8.8,\n \"S\u00e3o Paulo\": 12.2,\n \"Istanbul\": 15.9,\n \"Lagos\": 15.9,\n \"Shanghai\": 21.9,\n}\n\nfig, ax = plt.subplots(layout=\"constrained\")\nax.barh(population.keys(), population.values())\nax.set_xlabel('Population (in millions)')\n\n# left-align all ticklabels\nfor ticklabel in ax.get_yticklabels():\n ticklabel.set_horizontalalignment(\"left\")\n\n# increase padding\nax.tick_params(\"y\", pad=70)" ] } ], "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.13.5" } }, "nbformat": 4, "nbformat_minor": 0 }