You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
<liclass="toctree-l2"><aclass="reference internal" href="../introductory/lifecycle.html">The Lifecycle of a Plot</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="../introductory/customizing.html">Customizing Matplotlib with style sheets and rcParams</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="pgf.html">Text rendering with XeLaTeX/LuaLaTeX via the <codeclass="docutils literal notranslate"><spanclass="pre">pgf</span></code> backend</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="usetex.html">Text rendering with LaTeX</a></li>
<spanid="sphx-glr-tutorials-text-text-props-py"></span><h1>Text properties and layout<aclass="headerlink" href="#text-properties-and-layout" title="Permalink to this heading">#</a></h1>
<p>Controlling properties of text and its layout with Matplotlib.</p>
<p><aclass="reference internal" href="../../api/text_api.html#matplotlib.text.Text" title="matplotlib.text.Text"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.text.Text</span></code></a> instances have a variety of properties which can be
<td><p><aclass="reference internal" href="../../api/_as_gen/matplotlib.patches.Rectangle.html#matplotlib.patches.Rectangle" title="matplotlib.patches.Rectangle"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Rectangle</span></code></a> prop dict plus key <codeclass="docutils literal notranslate"><spanclass="pre">'pad'</span></code> which is a pad in points</p></td>
<p>You can lay out text with the alignment arguments
<codeclass="docutils literal notranslate"><spanclass="pre">horizontalalignment</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">verticalalignment</span></code>, and
<codeclass="docutils literal notranslate"><spanclass="pre">multialignment</span></code>. <codeclass="docutils literal notranslate"><spanclass="pre">horizontalalignment</span></code> controls whether the x
positional argument for the text indicates the left, center or right
side of the text bounding box. <codeclass="docutils literal notranslate"><spanclass="pre">verticalalignment</span></code> controls whether
the y positional argument for the text indicates the bottom, center or
top side of the text bounding box. <codeclass="docutils literal notranslate"><spanclass="pre">multialignment</span></code>, for newline
separated strings only, controls whether the different lines are left,
center or right justified. Here is an example which uses the
<aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.text.html#matplotlib.pyplot.text" title="matplotlib.pyplot.text"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">text()</span></code></a> command to show the various alignment
possibilities. The use of <codeclass="docutils literal notranslate"><spanclass="pre">transform=ax.transAxes</span></code> throughout the
code indicates that the coordinates are given relative to the axes
bounding box, with (0, 0) being the lower left of the axes and (1, 1) the
<td><p>Default font size in points. Relative font sizes
(<codeclass="docutils literal notranslate"><spanclass="pre">'large'</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">'x-small'</span></code>) are computed against
this size.</p></td>
</tr>
</tbody>
</table>
<p>Matplotlib can use font families installed on the user's computer, i.e.
Helvetica, Times, etc. Font families can also be specified with
generic-family aliases like (<codeclass="docutils literal notranslate"><spanclass="pre">{'cursive',</span><spanclass="pre">'fantasy',</span><spanclass="pre">'monospace',</span>
<p>If any of generic family names appear in <codeclass="docutils literal notranslate"><spanclass="pre">'font.family'</span></code>, we replace that entry
by all the entries in the corresponding rcParam mapping.
<spanid="font-nonlatin"></span><h2>Text with non-latin glyphs<aclass="headerlink" href="#text-with-non-latin-glyphs" title="Permalink to this heading">#</a></h2>
<p>As of v2.0 the <aclass="reference internal" href="../../users/prev_whats_new/dflt_style_changes.html#default-changes-font"><spanclass="std std-ref">default font</span></a>, DejaVu, contains
glyphs for many western alphabets, but not other scripts, such as Chinese,
Korean, or Japanese.</p>
<p>To set the default font to be one that supports the code points you
need, prepend the font name to <codeclass="docutils literal notranslate"><spanclass="pre">'font.family'</span></code> (recommended), or to the
desired alias lists.</p>
<divclass="highlight-default notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># first method</span>
<spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">rcParams</span><spanclass="p">[</span><spanclass="s1">'font.family'</span><spanclass="p">]</span><spanclass="o">=</span><spanclass="p">[</span><spanclass="s1">'Source Han Sans TW'</span><spanclass="p">,</span><spanclass="s1">'sans-serif'</span><spanclass="p">]</span>
<spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">rcParams</span><spanclass="p">[</span><spanclass="s1">'sans-serif'</span><spanclass="p">]</span><spanclass="o">=</span><spanclass="p">[</span><spanclass="s1">'Source Han Sans TW'</span><spanclass="p">,</span><spanclass="o">...</span><spanclass="p">]</span>
</pre></div>
</div>
<p>The generic family alias lists contain fonts that are either shipped
alongside Matplotlib (so they have 100% chance of being found), or fonts
which have a very high probability of being present in most systems.</p>
<p>A good practice when setting custom font families is to append
a generic-family to the font-family list as a last resort.</p>
<p>You can also set it in your <codeclass="file docutils literal notranslate"><spanclass="pre">.matplotlibrc</span></code> file:</p>