{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Text alignment\n\nTexts are aligned relative to their anchor point depending on the properties\n``horizontalalignment`` (default: ``left``) and ``verticalalignment``\n(default: ``baseline``.)\n\n.. redirect-from:: /gallery/pyplots/text_layout\n\n.. plot::\n\n    import matplotlib.pyplot as plt\n    import numpy as np\n\n    y = [0.22, 0.34, 0.5, 0.56, 0.78]\n    x = [0.17, 0.5, 0.855]\n    X, Y = np.meshgrid(x, y)\n\n    fig, ax = plt.subplots(figsize=(6, 4), dpi=100)\n    ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[])\n    ax.spines[:].set_visible(False)\n    ax.text(0.5, 0.5, 'plot', fontsize=128, ha='center', va='center', zorder=1)\n    ax.hlines(y, x[0], x[-1], color='grey')\n    ax.vlines(x, y[0], y[-1], color='grey')\n    ax.plot(X.ravel(), Y.ravel(), 'o')\n    pad_x = 0.02\n    pad_y = 0.04\n    ax.text(x[0] - pad_x, y[0], 'bottom', ha='right', va='center')\n    ax.text(x[0] - pad_x, y[1], 'baseline', ha='right', va='center')\n    ax.text(x[0] - pad_x, y[2], 'center', ha='right', va='center')\n    ax.text(x[0] - pad_x, y[3], 'center_baseline', ha='right', va='center')\n    ax.text(x[0] - pad_x, y[4], 'top', ha='right', va='center')\n    ax.text(x[0], y[0] - pad_y, 'left', ha='center', va='top')\n    ax.text(x[1], y[0] - pad_y, 'center', ha='center', va='top')\n    ax.text(x[2], y[0] - pad_y, 'right', ha='center', va='top')\n    ax.set_xlabel('horizontalalignment', fontsize=14)\n    ax.set_ylabel('verticalalignment', fontsize=14, labelpad=35)\n    ax.set_title(\n        'Relative position of text anchor point depending on alignment')\n    plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following plot uses this to align text relative to a plotted rectangle.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfig, ax = plt.subplots()\n\n# Build a rectangle in axes coords\nleft, width = .25, .5\nbottom, height = .25, .5\nright = left + width\ntop = bottom + height\np = plt.Rectangle((left, bottom), width, height, fill=False)\np.set_transform(ax.transAxes)\np.set_clip_on(False)\nax.add_patch(p)\n\nax.text(left, bottom, 'left top',\n        horizontalalignment='left',\n        verticalalignment='top',\n        transform=ax.transAxes)\n\nax.text(left, bottom, 'left bottom',\n        horizontalalignment='left',\n        verticalalignment='bottom',\n        transform=ax.transAxes)\n\nax.text(right, top, 'right bottom',\n        horizontalalignment='right',\n        verticalalignment='bottom',\n        transform=ax.transAxes)\n\nax.text(right, top, 'right top',\n        horizontalalignment='right',\n        verticalalignment='top',\n        transform=ax.transAxes)\n\nax.text(right, bottom, 'center top',\n        horizontalalignment='center',\n        verticalalignment='top',\n        transform=ax.transAxes)\n\nax.text(left, 0.5 * (bottom + top), 'right center',\n        horizontalalignment='right',\n        verticalalignment='center',\n        rotation='vertical',\n        transform=ax.transAxes)\n\nax.text(left, 0.5 * (bottom + top), 'left center',\n        horizontalalignment='left',\n        verticalalignment='center',\n        rotation='vertical',\n        transform=ax.transAxes)\n\nax.text(0.5 * (left + right), 0.5 * (bottom + top), 'middle',\n        horizontalalignment='center',\n        verticalalignment='center',\n        transform=ax.transAxes)\n\nax.text(right, 0.5 * (bottom + top), 'centered',\n        horizontalalignment='center',\n        verticalalignment='center',\n        rotation='vertical',\n        transform=ax.transAxes)\n\nax.text(left, top, 'rotated\\nwith newlines',\n        horizontalalignment='center',\n        verticalalignment='center',\n        rotation=45,\n        transform=ax.transAxes)\n\nax.set_axis_off()\n\nplt.show()"
      ]
    }
  ],
  "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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}