{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Text rotation mode\n\nThis example illustrates the effect of ``rotation_mode`` on the positioning\nof rotated text.\n\nRotated `.Text`\\s are created by passing the parameter ``rotation`` to\nthe constructor or the Axes' method `~.axes.Axes.text`.\n\nThe actual positioning depends on the additional parameters\n``horizontalalignment``, ``verticalalignment`` and ``rotation_mode``.\n``rotation_mode`` determines the order of rotation and alignment:\n\n- ``rotation_mode='default'`` (or None) first rotates the text and then aligns\n  the bounding box of the rotated text.\n- ``rotation_mode='anchor'`` aligns the unrotated text and then rotates the\n  text around the point of alignment.\n\n.. redirect-from:: /gallery/text_labels_and_annotations/text_rotation\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\n\ndef test_rotation_mode(fig, mode):\n    ha_list = [\"left\", \"center\", \"right\"]\n    va_list = [\"top\", \"center\", \"baseline\", \"bottom\"]\n    axs = fig.subplots(len(va_list), len(ha_list), sharex=True, sharey=True,\n                       subplot_kw=dict(aspect=1),\n                       gridspec_kw=dict(hspace=0, wspace=0))\n\n    # labels and title\n    for ha, ax in zip(ha_list, axs[-1, :]):\n        ax.set_xlabel(ha)\n    for va, ax in zip(va_list, axs[:, 0]):\n        ax.set_ylabel(va)\n    axs[0, 1].set_title(f\"rotation_mode='{mode}'\", size=\"large\")\n\n    kw = (\n        {} if mode == \"default\" else\n        {\"bbox\": dict(boxstyle=\"square,pad=0.\", ec=\"none\", fc=\"C1\", alpha=0.3)}\n    )\n\n    texts = {}\n\n    # use a different text alignment in each Axes\n    for i, va in enumerate(va_list):\n        for j, ha in enumerate(ha_list):\n            ax = axs[i, j]\n            # prepare Axes layout\n            ax.set(xticks=[], yticks=[])\n            ax.axvline(0.5, color=\"skyblue\", zorder=0)\n            ax.axhline(0.5, color=\"skyblue\", zorder=0)\n            ax.plot(0.5, 0.5, color=\"C0\", marker=\"o\", zorder=1)\n            # add text with rotation and alignment settings\n            tx = ax.text(0.5, 0.5, \"Tpg\",\n                         size=\"x-large\", rotation=40,\n                         horizontalalignment=ha, verticalalignment=va,\n                         rotation_mode=mode, **kw)\n            texts[ax] = tx\n\n    if mode == \"default\":\n        # highlight bbox\n        fig.canvas.draw()\n        for ax, text in texts.items():\n            bb = text.get_window_extent().transformed(ax.transData.inverted())\n            rect = plt.Rectangle((bb.x0, bb.y0), bb.width, bb.height,\n                                 facecolor=\"C1\", alpha=0.3, zorder=2)\n            ax.add_patch(rect)\n\n\nfig = plt.figure(figsize=(8, 5))\nsubfigs = fig.subfigures(1, 2)\ntest_rotation_mode(subfigs[0], \"default\")\ntest_rotation_mode(subfigs[1], \"anchor\")\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.text` / `matplotlib.pyplot.text`\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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}