<divid="unreleased-message"> You are reading an old version of the documentation (v2.1.1). For the latest version see <ahref="/stable/">https://matplotlib.org/stable/</a></div>
<spanid="sphx-glr-gallery-api-font-file-py"></span><h1>Using a ttf font file in Matplotlib<aclass="headerlink" href="#using-a-ttf-font-file-in-matplotlib" title="Permalink to this headline">¶</a></h1>
<p>Although it is usually not a good idea to explicitly point to a single ttf file
for a font instance, you can do so using the <codeclass="xref py py-obj docutils literal"><spanclass="pre">font_manager.FontProperties</span></code>
<em>fname</em> argument.</p>
<p>Here, we use the Computer Modern roman font (<codeclass="docutils literal"><spanclass="pre">cmr10</span></code>) shipped with
Matplotlib.</p>
<p>For a more flexible solution, see <aclass="reference internal" href="font_family_rc_sgskip.html"><spanclass="doc">Configuring the font family</span></a> and
<spanclass="kn">from</span><spanclass="nn">matplotlib</span><spanclass="kn">import</span><spanclass="n">font_manager</span><spanclass="k">as</span><spanclass="n">fm</span><spanclass="p">,</span><spanclass="n">pyplot</span><spanclass="k">as</span><spanclass="n">plt</span><spanclass="p">,</span><ahref="../../api/matplotlib_configuration_api.html#matplotlib.rcParams" title="View documentation for matplotlib.rcParams"><spanclass="n">rcParams</span></a>
<spanclass="n">fig</span><spanclass="p">,</span><spanclass="n">ax</span><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">()</span>
<spanclass="n">fpath</span><spanclass="o">=</span><spanclass="n">os</span><spanclass="o">.</span><spanclass="n">path</span><spanclass="o">.</span><spanclass="n">join</span><spanclass="p">(</span><ahref="../../api/matplotlib_configuration_api.html#matplotlib.rcParams" title="View documentation for matplotlib.rcParams"><spanclass="n">rcParams</span></a><spanclass="p">[</span><spanclass="s2">"datapath"</span><spanclass="p">],</span><spanclass="s2">"fonts/ttf/cmr10.ttf"</span><spanclass="p">)</span>
<spanclass="n">prop</span><spanclass="o">=</span><ahref="../../api/font_manager_api.html#matplotlib.font_manager.FontProperties" title="View documentation for matplotlib.font_manager.FontProperties"><spanclass="n">fm</span><spanclass="o">.</span><spanclass="n">FontProperties</span></a><spanclass="p">(</span><spanclass="n">fname</span><spanclass="o">=</span><spanclass="n">fpath</span><spanclass="p">)</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">set_title</span><spanclass="p">(</span><spanclass="s1">'This is a special font: {}'</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="n">fname</span><spanclass="p">),</span><spanclass="n">fontproperties</span><spanclass="o">=</span><spanclass="n">prop</span><spanclass="p">)</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">set_xlabel</span><spanclass="p">(</span><spanclass="s1">'This is the default font'</span><spanclass="p">)</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
</pre></div>
</div>
<p><strong>Total running time of the script:</strong> ( 0 minutes 0.016 seconds)</p>