{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n.. redirect-from:: /tutorials/text/text_props\n\n\n# Text properties and layout\n\nControlling properties of text and its layout with Matplotlib.\n\n`matplotlib.text.Text` instances have a variety of properties which can be\nconfigured via keyword arguments to `~.Axes.set_title`, `~.Axes.set_xlabel`,\n`~.Axes.text`, etc.\n\n========================== ======================================================================================================================\nProperty Value Type\n========================== ======================================================================================================================\nalpha `float`\nbackgroundcolor any matplotlib `color `\nbbox `~matplotlib.patches.Rectangle` prop dict plus key ``'pad'`` which is a pad in points\nclip_box a matplotlib.transform.Bbox instance\nclip_on bool\nclip_path a `~matplotlib.path.Path` instance and a `~matplotlib.transforms.Transform` instance, a `~matplotlib.patches.Patch`\ncolor any matplotlib `color `\nfamily [ ``'serif'`` | ``'sans-serif'`` | ``'cursive'`` | ``'fantasy'`` | ``'monospace'`` ]\nfontproperties `~matplotlib.font_manager.FontProperties`\nhorizontalalignment or ha [ ``'center'`` | ``'right'`` | ``'left'`` ]\nlabel any string\nlinespacing `float`\nmultialignment [``'left'`` | ``'right'`` | ``'center'`` ]\nname or fontname string e.g., [``'Sans'`` | ``'Courier'`` | ``'Helvetica'`` ...]\npicker [None|float|bool|callable]\nposition (x, y)\nrotation [ angle in degrees | ``'vertical'`` | ``'horizontal'`` ]\nsize or fontsize [ size in points | relative size, e.g., ``'smaller'``, ``'x-large'`` ]\nstyle or fontstyle [ ``'normal'`` | ``'italic'`` | ``'oblique'`` ]\ntext string or anything printable with '%s' conversion\ntransform `~matplotlib.transforms.Transform` subclass\nvariant [ ``'normal'`` | ``'small-caps'`` ]\nverticalalignment or va [ ``'center'`` | ``'top'`` | ``'bottom'`` | ``'baseline'`` ]\nvisible bool\nweight or fontweight [ ``'normal'`` | ``'bold'`` | ``'heavy'`` | ``'light'`` | ``'ultrabold'`` | ``'ultralight'``]\nx `float`\ny `float`\nzorder any number\n========================== ======================================================================================================================\n\n\nYou can lay out text with the alignment arguments\n``horizontalalignment``, ``verticalalignment``, and\n``multialignment``. ``horizontalalignment`` controls whether the x\npositional argument for the text indicates the left, center or right\nside of the text bounding box. ``verticalalignment`` controls whether\nthe y positional argument for the text indicates the bottom, center or\ntop side of the text bounding box. ``multialignment``, for newline\nseparated strings only, controls whether the different lines are left,\ncenter or right justified. Here is an example which uses the\n:func:`~matplotlib.pyplot.text` command to show the various alignment\npossibilities. The use of ``transform=ax.transAxes`` throughout the\ncode indicates that the coordinates are given relative to the Axes\nbounding box, with (0, 0) being the lower left of the Axes and (1, 1) the\nupper right.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nimport matplotlib.patches as patches\n\n# build a rectangle in axes coords\nleft, width = .25, .5\nbottom, height = .25, .5\nright = left + width\ntop = bottom + height\n\nfig = plt.figure()\nax = fig.add_axes([0, 0, 1, 1])\n\n# axes coordinates: (0, 0) is bottom left and (1, 1) is upper right\np = patches.Rectangle(\n (left, bottom), width, height,\n fill=False, transform=ax.transAxes, clip_on=False\n )\n\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 fontsize=20, color='red',\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()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Default Font\n\nThe base default font is controlled by a set of rcParams. To set the font\nfor mathematical expressions, use the rcParams beginning with ``mathtext``\n(see `mathtext `).\n\n+---------------------+----------------------------------------------------+\n| rcParam | usage |\n+=====================+====================================================+\n| ``'font.family'`` | List of font families (installed on user's machine)|\n| | and/or ``{'cursive', 'fantasy', 'monospace', |\n| | 'sans', 'sans serif', 'sans-serif', 'serif'}``. |\n| | |\n+---------------------+----------------------------------------------------+\n| ``'font.style'`` | The default style, ex ``'normal'``, |\n| | ``'italic'``. |\n| | |\n+---------------------+----------------------------------------------------+\n| ``'font.variant'`` | Default variant, ex ``'normal'``, ``'small-caps'`` |\n| | (untested) |\n+---------------------+----------------------------------------------------+\n| ``'font.stretch'`` | Default stretch, ex ``'normal'``, ``'condensed'`` |\n| | (incomplete) |\n| | |\n+---------------------+----------------------------------------------------+\n| ``'font.weight'`` | Default weight. Either string or integer |\n| | |\n| | |\n+---------------------+----------------------------------------------------+\n| ``'font.size'`` | Default font size in points. Relative font sizes |\n| | (``'large'``, ``'x-small'``) are computed against |\n| | this size. |\n+---------------------+----------------------------------------------------+\n\nMatplotlib can use font families installed on the user's computer, i.e.\nHelvetica, Times, etc. Font families can also be specified with\ngeneric-family aliases like (``{'cursive', 'fantasy', 'monospace',\n'sans', 'sans serif', 'sans-serif', 'serif'}``).\n\n

Note

To access the full list of available fonts: ::\n\n matplotlib.font_manager.get_font_names()

\n\nThe mapping between the generic family aliases and actual font families\n(mentioned at `default rcParams `)\nis controlled by the following rcParams:\n\n\n+------------------------------------------+--------------------------------+\n| CSS-based generic-family alias | rcParam with mappings |\n+==========================================+================================+\n| ``'serif'`` | ``'font.serif'`` |\n+------------------------------------------+--------------------------------+\n| ``'monospace'`` | ``'font.monospace'`` |\n+------------------------------------------+--------------------------------+\n| ``'fantasy'`` | ``'font.fantasy'`` |\n+------------------------------------------+--------------------------------+\n| ``'cursive'`` | ``'font.cursive'`` |\n+------------------------------------------+--------------------------------+\n| ``{'sans', 'sans serif', 'sans-serif'}`` | ``'font.sans-serif'`` |\n+------------------------------------------+--------------------------------+\n\n\nIf any of generic family names appear in ``'font.family'``, we replace that entry\nby all the entries in the corresponding rcParam mapping.\nFor example: ::\n\n matplotlib.rcParams['font.family'] = ['Family1', 'serif', 'Family2']\n matplotlib.rcParams['font.serif'] = ['SerifFamily1', 'SerifFamily2']\n\n # This is effectively translated to:\n matplotlib.rcParams['font.family'] = ['Family1', 'SerifFamily1', 'SerifFamily2', 'Family2']\n\n\n\n## Text with non-latin glyphs\n\nAs of v2.0 the `default font `, DejaVu, contains\nglyphs for many western alphabets, but not other scripts, such as Chinese,\nKorean, or Japanese.\n\nTo set the default font to be one that supports the code points you\nneed, prepend the font name to ``'font.family'`` (recommended), or to the\ndesired alias lists. ::\n\n # first method\n matplotlib.rcParams['font.family'] = ['Source Han Sans TW', 'sans-serif']\n\n # second method\n matplotlib.rcParams['font.family'] = ['sans-serif']\n matplotlib.rcParams['sans-serif'] = ['Source Han Sans TW', ...]\n\nThe generic family alias lists contain fonts that are either shipped\nalongside Matplotlib (so they have 100% chance of being found), or fonts\nwhich have a very high probability of being present in most systems.\n\nA good practice when setting custom font families is to append\na generic-family to the font-family list as a last resort.\n\nYou can also set it in your :file:`.matplotlibrc` file::\n\n font.family: Source Han Sans TW, Arial, sans-serif\n\nTo control the font used on per-artist basis use the *name*, *fontname* or\n*fontproperties* keyword arguments documented in `text_props`.\n\n\nOn linux, [fc-list](https://linux.die.net/man/1/fc-list)_ can be a\nuseful tool to discover the font name; for example ::\n\n $ fc-list :lang=zh family\n Noto to Sans Mono CJK TC,Noto Sans Mono CJK TC Bold\n Noto Sans CJK TC,Noto Sans CJK TC Medium\n Noto Sans CJK TC,Noto Sans CJK TC DemiLight\n Noto Sans CJK KR,Noto Sans CJK KR Black\n Noto Sans CJK TC,Noto Sans CJK TC Black\n Noto Sans Mono CJK TC,Noto Sans Mono CJK TC Regular\n Noto Sans CJK SC,Noto Sans CJK SC Light\n\nlists all of the fonts that support Chinese.\n\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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }