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="sphx-glr-tutorials-colors-colormaps-py"></span><h1>Choosing Colormaps in Matplotlib<aclass="headerlink" href="#choosing-colormaps-in-matplotlib" title="Permalink to this heading">#</a></h1>
<p>Matplotlib has a number of built-in colormaps accessible via
<aclass="reference internal" href="../../api/matplotlib_configuration_api.html#matplotlib.colormaps" title="matplotlib.colormaps"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.colormaps</span></code></a>. There are also external libraries that
have many extra colormaps, which can be viewed in the
<aclass="reference external" href="https://matplotlib.org/mpl-third-party/#colormaps-and-styles">Third-party colormaps</a> section of the Matplotlib documentation.
Here we briefly discuss how to choose between the many options. For
help on creating your own colormaps, see
<aclass="reference internal" href="colormap-manipulation.html"><spanclass="doc">Creating Colormaps in Matplotlib</span></a>.</p>
<sectionid="overview">
<h2>Overview<aclass="headerlink" href="#overview" title="Permalink to this heading">#</a></h2>
<p>The idea behind choosing a good colormap is to find a good representation in 3D
colorspace for your data set. The best colormap for any given data set depends
on many things including:</p>
<ulclass="simple">
<li><p>Whether representing form or metric data (<aclass="reference internal" href="#ware" id="id1"><span>[Ware]</span></a>)</p></li>
<li><p>Your knowledge of the data set (<em>e.g.</em>, is there a critical value
from which the other values deviate?)</p></li>
<li><p>If there is an intuitive color scheme for the parameter you are plotting</p></li>
<li><p>If there is a standard in the field the audience may be expecting</p></li>
</ul>
<p>For many applications, a perceptually uniform colormap is the best choice;
i.e. a colormap in which equal steps in data are perceived as equal
steps in the color space. Researchers have found that the human brain
perceives changes in the lightness parameter as changes in the data
much better than, for example, changes in hue. Therefore, colormaps
which have monotonically increasing lightness through the colormap
will be better interpreted by the viewer. Wonderful examples of
perceptually uniform colormaps can be found in the
<aclass="reference external" href="https://matplotlib.org/mpl-third-party/#colormaps-and-styles">Third-party colormaps</a> section as well.</p>
<p>Color can be represented in 3D space in various ways. One way to represent color
is using CIELAB. In CIELAB, color space is represented by lightness,
<spanclass="math notranslate nohighlight">\(L^*\)</span>; red-green, <spanclass="math notranslate nohighlight">\(a^*\)</span>; and yellow-blue, <spanclass="math notranslate nohighlight">\(b^*\)</span>. The lightness
parameter <spanclass="math notranslate nohighlight">\(L^*\)</span> can then be used to learn more about how the matplotlib
colormaps will be perceived by viewers.</p>
<p>An excellent starting resource for learning about human perception of colormaps
is from <aclass="reference internal" href="#ibm" id="id2"><span>[IBM]</span></a>.</p>
</section>
<sectionid="classes-of-colormaps">
<spanid="color-colormaps-reference"></span><h2>Classes of colormaps<aclass="headerlink" href="#classes-of-colormaps" title="Permalink to this heading">#</a></h2>
<p>Colormaps are often split into several categories based on their function (see,
<h3>Sequential<aclass="headerlink" href="#sequential" title="Permalink to this heading">#</a></h3>
<p>For the Sequential plots, the lightness value increases monotonically through
the colormaps. This is good. Some of the <spanclass="math notranslate nohighlight">\(L^*\)</span> values in the colormaps
span from 0 to 100 (binary and the other grayscale), and others start around
<spanclass="math notranslate nohighlight">\(L^*=20\)</span>. Those that have a smaller range of <spanclass="math notranslate nohighlight">\(L^*\)</span> will accordingly
have a smaller perceptual range. Note also that the <spanclass="math notranslate nohighlight">\(L^*\)</span> function varies
amongst the colormaps: some are approximately linear in <spanclass="math notranslate nohighlight">\(L^*\)</span> and others
<imgsrc="../../_images/sphx_glr_colormaps_002.png" srcset="../../_images/sphx_glr_colormaps_002.png, ../../_images/sphx_glr_colormaps_002_2_0x.png 2.0x" alt="Sequential colormaps" class = "sphx-glr-single-img"/></section>
<sectionid="sequential2">
<h3>Sequential2<aclass="headerlink" href="#sequential2" title="Permalink to this heading">#</a></h3>
<p>Many of the <spanclass="math notranslate nohighlight">\(L^*\)</span> values from the Sequential2 plots are monotonically
increasing, but some (autumn, cool, spring, and winter) plateau or even go both
up and down in <spanclass="math notranslate nohighlight">\(L^*\)</span> space. Others (afmhot, copper, gist_heat, and hot)
have kinks in the <spanclass="math notranslate nohighlight">\(L^*\)</span> functions. Data that is being represented in a
region of the colormap that is at a plateau or kink will lead to a perception of
banding of the data in those values in the colormap (see <aclass="reference internal" href="#mycarta-banding" id="id4"><span>[mycarta-banding]</span></a> for
<imgsrc="../../_images/sphx_glr_colormaps_004.png" srcset="../../_images/sphx_glr_colormaps_004.png, ../../_images/sphx_glr_colormaps_004_2_0x.png 2.0x" alt="Diverging colormaps" class = "sphx-glr-single-img"/></section>
<sectionid="cyclic">
<h3>Cyclic<aclass="headerlink" href="#cyclic" title="Permalink to this heading">#</a></h3>
<p>For Cyclic maps, we want to start and end on the same color, and meet a
symmetric center point in the middle. <spanclass="math notranslate nohighlight">\(L^*\)</span> should change monotonically
from start to middle, and inversely from middle to end. It should be symmetric
on the increasing and decreasing side, and only differ in hue. At the ends and
middle, <spanclass="math notranslate nohighlight">\(L^*\)</span> will reverse direction, which should be smoothed in
<spanclass="math notranslate nohighlight">\(L^*\)</span> space to reduce artifacts. See <aclass="reference internal" href="#kovesi-colormaps" id="id5"><span>[kovesi-colormaps]</span></a> for more
information on the design of cyclic maps.</p>
<p>The often-used HSV colormap is included in this set of colormaps, although it
is not symmetric to a center point. Additionally, the <spanclass="math notranslate nohighlight">\(L^*\)</span> values vary
widely throughout the colormap, making it a poor choice for representing data
for viewers to see perceptually. See an extension on this idea at
<imgsrc="../../_images/sphx_glr_colormaps_006.png" srcset="../../_images/sphx_glr_colormaps_006.png, ../../_images/sphx_glr_colormaps_006_2_0x.png 2.0x" alt="Qualitative colormaps" class = "sphx-glr-single-img"/></section>
<sectionid="miscellaneous">
<h3>Miscellaneous<aclass="headerlink" href="#miscellaneous" title="Permalink to this heading">#</a></h3>
<p>Some of the miscellaneous colormaps have particular uses for which
they have been created. For example, gist_earth, ocean, and terrain
all seem to be created for plotting topography (green/brown) and water
depths (blue) together. We would expect to see a divergence in these
colormaps, then, but multiple kinks may not be ideal, such as in
gist_earth and terrain. CMRmap was created to convert well to
grayscale, though it does appear to have some small kinks in
<spanclass="math notranslate nohighlight">\(L^*\)</span>. cubehelix was created to vary smoothly in both lightness
and hue, but appears to have a small hump in the green hue area. turbo
was created to display depth and disparity data.</p>
<p>The often-used jet colormap is included in this set of colormaps. We can see
that the <spanclass="math notranslate nohighlight">\(L^*\)</span> values vary widely throughout the colormap, making it a
poor choice for representing data for viewers to see perceptually. See an
extension on this idea at <aclass="reference internal" href="#mycarta-jet" id="id7"><span>[mycarta-jet]</span></a> and <aclass="reference internal" href="#turbo" id="id8"><span>[turbo]</span></a>.</p>
<ahref="https://docs.python.org/3/library/stdtypes.html#list" title="builtins.list" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">locs</span></a><spanclass="o">=</span><spanclass="p">[]</span><spanclass="c1"># locations for text labels</span>