.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_color_colorbar_basics.py: ======== Colorbar ======== Use `~.figure.Figure.colorbar` by specifying the mappable object (here the `~.matplotlib.image.AxesImage` returned by `~.axes.Axes.imshow`) and the axes to attach the colorbar to. .. code-block:: python import numpy as np import matplotlib.pyplot as plt # setup some generic data N = 37 x, y = np.mgrid[:N, :N] Z = (np.cos(x*0.2) + np.sin(y*0.3)) # mask out the negative and positive values, respectively Zpos = np.ma.masked_less(Z, 0) Zneg = np.ma.masked_greater(Z, 0) fig, (ax1, ax2) = plt.subplots(figsize=(8, 3), ncols=2) # plot just the positive data and save the # color "mappable" object returned by ax1.imshow pos = ax1.imshow(Zpos, cmap='Blues', interpolation='none') # add the colorbar using the figure's method, # telling which mappable we're talking about and # which axes object it should be near fig.colorbar(pos, ax=ax1) # repeat everything above for the negative data neg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none') fig.colorbar(neg, ax=ax2) plt.show() .. image:: /gallery/color/images/sphx_glr_colorbar_basics_001.png :class: sphx-glr-single-img ------------ References """""""""" The use of the following functions, methods, classes and modules is shown in this example: .. code-block:: python import matplotlib matplotlib.axes.Axes.imshow matplotlib.pyplot.imshow matplotlib.figure.Figure.colorbar matplotlib.pyplot.colorbar .. _sphx_glr_download_gallery_color_colorbar_basics.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: colorbar_basics.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: colorbar_basics.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature Keywords: matplotlib code example, codex, python plot, pyplot `Gallery generated by Sphinx-Gallery `_