{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Text rotation angle in data coordinates\n\nText objects in matplotlib are normally rotated with respect to the\nscreen coordinate system (i.e., 45 degrees rotation plots text along a\nline that is in between horizontal and vertical no matter how the axes\nare changed). However, at times one wants to rotate text with respect\nto something on the plot. In this case, the correct angle won't be\nthe angle of that object in the plot coordinate system, but the angle\nthat that object APPEARS in the screen coordinate system. This angle\ncan be determined automatically by setting the parameter\n*transform_rotates_text*, as shown in the example below.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfig, ax = plt.subplots()\n\n# Plot diagonal line (45 degrees in data coordinates)\nax.plot(range(0, 8), range(0, 8))\nax.set_xlim([-10, 10])\n\n# Plot text\nax.text(-8, 0, 'text 45\u00b0 in screen coordinates', fontsize=18,\n rotation=45, rotation_mode='anchor')\nax.text(0, 0, 'text 45\u00b0 in data coordinates', fontsize=18,\n rotation=45, rotation_mode='anchor',\n transform_rotates_text=True)\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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }