.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/images_contours_and_fields/contourf_log.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_images_contours_and_fields_contourf_log.py: ============================ Contourf and log color scale ============================ Demonstrate use of a log color scale in contourf .. GENERATED FROM PYTHON SOURCE LINES 8-52 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from numpy import ma from matplotlib import cm, ticker N = 100 x = np.linspace(-3.0, 3.0, N) y = np.linspace(-2.0, 2.0, N) X, Y = np.meshgrid(x, y) # A low hump with a spike coming out. # Needs to have z/colour axis on a log scale, so we see both hump and spike. # A linear scale only shows the spike. Z1 = np.exp(-X**2 - Y**2) Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2) z = Z1 + 50 * Z2 # Put in some negative values (lower left corner) to cause trouble with logs: z[:5, :5] = -1 # The following is not strictly essential, but it will eliminate # a warning. Comment it out to see the warning. z = ma.masked_where(z <= 0, z) # Automatic selection of levels works; setting the # log locator tells contourf to use a log scale: fig, ax = plt.subplots() cs = ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r) # Alternatively, you can manually set the levels # and the norm: # lev_exp = np.arange(np.floor(np.log10(z.min())-1), # np.ceil(np.log10(z.max())+1)) # levs = np.power(10, lev_exp) # cs = ax.contourf(X, Y, z, levs, norm=colors.LogNorm()) cbar = fig.colorbar(cs) plt.show() .. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_contourf_log_001.png :alt: contourf log :srcset: /gallery/images_contours_and_fields/images/sphx_glr_contourf_log_001.png, /gallery/images_contours_and_fields/images/sphx_glr_contourf_log_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 53-62 .. admonition:: References The use of the following functions, methods, classes and modules is shown in this example: - `matplotlib.axes.Axes.contourf` / `matplotlib.pyplot.contourf` - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar` - `matplotlib.axes.Axes.legend` / `matplotlib.pyplot.legend` - `matplotlib.ticker.LogLocator` .. _sphx_glr_download_gallery_images_contours_and_fields_contourf_log.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: contourf_log.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: contourf_log.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_