{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Configure the font family\n\nYou can explicitly set which font family is picked up, either by specifying\nfamily names of fonts installed on user's system, or generic-families\n(e.g., 'serif', 'sans-serif', 'monospace', 'fantasy' or 'cursive'),\nor a combination of both.\n(see `text_props`)\n\nIn the example below, we are overriding the default sans-serif generic family\nto include a specific (Tahoma) font. (Note that the best way to achieve this\nwould simply be to prepend 'Tahoma' in 'font.family')\n\nThe default family is set with the font.family rcparam,\ne.g. ::\n\n rcParams['font.family'] = 'sans-serif'\n\nand for the font.family you set a list of font styles to try to find\nin order::\n\n rcParams['font.sans-serif'] = ['Tahoma', 'DejaVu Sans',\n 'Lucida Grande', 'Verdana']\n\n.. redirect-from:: /gallery/font_family_rc_sgskip\n\nThe ``font.family`` defaults are OS dependent and can be viewed with:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nprint(plt.rcParams[\"font.sans-serif\"][0])\nprint(plt.rcParams[\"font.monospace\"][0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose default sans-serif font\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def print_text(text):\n fig, ax = plt.subplots(figsize=(6, 1), facecolor=\"#eefade\")\n ax.text(0.5, 0.5, text, ha='center', va='center', size=40)\n ax.axis(\"off\")\n plt.show()\n\n\nplt.rcParams[\"font.family\"] = \"sans-serif\"\nprint_text(\"Hello World! 01\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose sans-serif font and specify to it to \"Nimbus Sans\"\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.rcParams[\"font.family\"] = \"sans-serif\"\nplt.rcParams[\"font.sans-serif\"] = [\"Nimbus Sans\"]\nprint_text(\"Hello World! 02\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose default monospace font\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.rcParams[\"font.family\"] = \"monospace\"\nprint_text(\"Hello World! 03\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose monospace font and specify to it to \"FreeMono\"\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.rcParams[\"font.family\"] = \"monospace\"\nplt.rcParams[\"font.monospace\"] = [\"FreeMono\"]\nprint_text(\"Hello World! 04\")" ] } ], "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 }