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
<spanid="id1"></span><h1>Customizing matplotlib<aclass="headerlink" href="#customizing-matplotlib" title="Permalink to this headline">¶</a></h1>
<divclass="section" id="using-style-sheets">
<h2>Using style sheets<aclass="headerlink" href="#using-style-sheets" title="Permalink to this headline">¶</a></h2>
<p>The <codeclass="docutils literal"><spanclass="pre">style</span></code> package adds support for easy-to-switch plotting “styles” with
the same parameters as a <aclass="reference external" href="http://matplotlib.org/users/customizing.html">matplotlibrc</a> file (which is read at startup to
configure matplotlib).</p>
<p>There are a number of pre-defined styles provided by matplotlib. For
example, there’s a pre-defined style called “ggplot”, which emulates the
aesthetics of <aclass="reference external" href="http://ggplot2.org/">ggplot</a> (a popular plotting package for <aclass="reference external" href="https://www.r-project.org/">R</a>). To use this style,
<h3>Defining your own style<aclass="headerlink" href="#defining-your-own-style" title="Permalink to this headline">¶</a></h3>
<p>You can create custom styles and use them by calling <codeclass="docutils literal"><spanclass="pre">style.use</span></code> with the
path or URL to the style sheet. Additionally, if you add your
<codeclass="docutils literal"><spanclass="pre"><style-name>.mplstyle</span></code> file to <codeclass="docutils literal"><spanclass="pre">mpl_configdir/stylelib</span></code>, you can reuse
your custom style sheet with a call to <codeclass="docutils literal"><spanclass="pre">style.use(<style-name>)</span></code>. By default
<codeclass="docutils literal"><spanclass="pre">mpl_configdir</span></code> should be <codeclass="docutils literal"><spanclass="pre">~/.config/matplotlib</span></code>, but you can check where
yours is with <codeclass="docutils literal"><spanclass="pre">matplotlib.get_configdir()</span></code>; you may need to create this
directory. You also can change the directory where matplotlib looks for
the stylelib/ folder by setting the MPLCONFIGDIR environment variable,
see <aclass="reference internal" href="../faq/troubleshooting_faq.html#locating-matplotlib-config-dir"><spanclass="std std-ref">matplotlib configuration and cache directory locations</span></a>.</p>
<p>Note that a custom style sheet in <codeclass="docutils literal"><spanclass="pre">mpl_configdir/stylelib</span></code> will
override a style sheet defined by matplotlib if the styles have the same name.</p>
<p>For example, you might want to create
<codeclass="docutils literal"><spanclass="pre">mpl_configdir/stylelib/presentation.mplstyle</span></code> with the following:</p>
<h2>matplotlib rcParams<aclass="headerlink" href="#matplotlib-rcparams" title="Permalink to this headline">¶</a></h2>
<divclass="section" id="dynamic-rc-settings">
<spanid="customizing-with-dynamic-rc-settings"></span><h3>Dynamic rc settings<aclass="headerlink" href="#dynamic-rc-settings" title="Permalink to this headline">¶</a></h3>
<p>You can also dynamically change the default rc settings in a python script or
interactively from the python shell. All of the rc settings are stored in a
dictionary-like variable called <aclass="reference internal" href="../api/matplotlib_configuration_api.html#matplotlib.rcParams" title="matplotlib.rcParams"><codeclass="xref py py-data docutils literal"><spanclass="pre">matplotlib.rcParams</span></code></a>, which is global to
the matplotlib package. rcParams can be modified directly, for example:</p>
<p>Matplotlib also provides a couple of convenience functions for modifying rc
settings. The <aclass="reference internal" href="../api/matplotlib_configuration_api.html#matplotlib.rc" title="matplotlib.rc"><codeclass="xref py py-func docutils literal"><spanclass="pre">matplotlib.rc()</span></code></a> command can be used to modify multiple
settings in a single group at once, using keyword arguments:</p>
<p>The <codeclass="xref py py-func docutils literal"><spanclass="pre">matplotlib.rcdefaults()</span></code> command will restore the standard matplotlib
default settings.</p>
<p>There is some degree of validation when setting the values of rcParams, see
<aclass="reference internal" href="../api/rcsetup_api.html#module-matplotlib.rcsetup" title="matplotlib.rcsetup"><codeclass="xref py py-mod docutils literal"><spanclass="pre">matplotlib.rcsetup</span></code></a> for details.</p>
</div>
<divclass="section" id="the-matplotlibrc-file">
<spanid="customizing-with-matplotlibrc-files"></span><h3>The <codeclass="file docutils literal"><spanclass="pre">matplotlibrc</span></code> file<aclass="headerlink" href="#the-matplotlibrc-file" title="Permalink to this headline">¶</a></h3>
<p>matplotlib uses <codeclass="file docutils literal"><spanclass="pre">matplotlibrc</span></code> configuration files to customize all kinds
of properties, which we call <codeclass="xref py py-obj docutils literal"><spanclass="pre">rc</span><spanclass="pre">settings</span></code> or <codeclass="xref py py-obj docutils literal"><spanclass="pre">rc</span><spanclass="pre">parameters</span></code>. You can control
the defaults of almost every property in matplotlib: figure size and dpi, line
width, color and style, axes, axis and grid properties, text and font
properties and so on. matplotlib looks for <codeclass="file docutils literal"><spanclass="pre">matplotlibrc</span></code> in four
locations, in the following order:</p>
<olclass="arabic">
<li><pclass="first"><codeclass="file docutils literal"><spanclass="pre">matplotlibrc</span></code> in the current working directory, usually used for
specific customizations that you do not want to apply elsewhere.</p>
<li><pclass="first">It next looks in a user-specific place, depending on your platform:</p>
<ulclass="simple">
<li>On Linux and FreeBSD, it looks in <codeclass="file docutils literal"><spanclass="pre">.config/matplotlib/matplotlibrc</span></code>
(or <codeclass="xref py py-obj docutils literal"><spanclass="pre">$XDG_CONFIG_HOME/matplotlib/matplotlibrc</span></code>) if you’ve customized
your environment.</li>
<li>On other platforms, it looks in <codeclass="file docutils literal"><spanclass="pre">.matplotlib/matplotlibrc</span></code>.</li>
</ul>
<p>See <aclass="reference internal" href="../faq/troubleshooting_faq.html#locating-matplotlib-config-dir"><spanclass="std std-ref">matplotlib configuration and cache directory locations</span></a>.</p>
</li>
<li><pclass="first"><codeclass="file docutils literal"><em><spanclass="pre">INSTALL</span></em><spanclass="pre">/matplotlib/mpl-data/matplotlibrc</span></code>, where
<codeclass="file docutils literal"><em><spanclass="pre">INSTALL</span></em></code> is something like
<codeclass="file docutils literal"><spanclass="pre">/usr/lib/python3.5/site-packages</span></code> on Linux, and maybe
<codeclass="file docutils literal"><spanclass="pre">C:\Python35\Lib\site-packages</span></code> on Windows. Every time you
install matplotlib, this file will be overwritten, so if you want
your customizations to be saved, please move this file to your
user-specific matplotlib directory.</p>
</li>
</ol>
<p>To display where the currently active <codeclass="file docutils literal"><spanclass="pre">matplotlibrc</span></code> file was
<spanid="matplotlibrc-sample"></span><h4>A sample matplotlibrc file<aclass="headerlink" href="#a-sample-matplotlibrc-file" title="Permalink to this headline">¶</a></h4>
<spanclass="c1"># expanded, extra-expanded, ultra-expanded, wider, and narrower. This</span>
<spanclass="c1"># property is not currently implemented.</span>
<spanclass="c1">#</span>
<spanclass="c1"># The font.size property is the default font size for text, given in pts.</span>
<spanclass="c1"># 10 pt is the standard value.</span>
<spanclass="c1">#</span>
<spanclass="c1">#font.family : sans-serif</span>
<spanclass="c1">#font.style : normal</span>
<spanclass="c1">#font.variant : normal</span>
<spanclass="c1">#font.weight : medium</span>
<spanclass="c1">#font.stretch : normal</span>
<spanclass="c1"># note that font.size controls default text sizes. To configure</span>
<spanclass="c1"># special text sizes tick labels, axes, labels, title, etc, see the rc</span>
<spanclass="c1"># settings for axes and ticks. Special text sizes can be defined</span>
<spanclass="c1"># relative to font.size, using the following values: xx-small, x-small,</span>
<spanclass="c1"># small, medium, large, x-large, xx-large, larger, or smaller</span>
<spanclass="c1">#font.size : 10.0</span>
<spanclass="c1">#font.serif : DejaVu Serif, Bitstream Vera Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif</span>
<spanclass="c1">#font.sans-serif : DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif</span>