Listing up sequential 2D colormap with one common colorbar using python and matplotlib.pyplot


The concept is:

Listing up sequential 2D colormap with one common colorbar using python and matplotlib.pyplot

When you want to show a 2D colormap in a sequence, the simplest way to do it is just listing up the output figure like the left side of the above image. However, common information, that is xticks and colorbars, should be omitted to show useful colormap as large as possible. This page shows how to align 2D colormaps without useless information. Three figures are drawn sequentially: 1. full image, 2. without colorbar, and 3. witiout colorbar and xticks. A standalone colorbar will be drawn, plotted and saved at the first iteration of the sequence. By properly selecting the output figure on your tools such as LaTex or Microsoft Office, you can obtain the figure like the right side of the above image.

See also:

Python Matplotlib Tips: Combine multiple line plot and contour plot with a colorbar using Python and matplotlib.pyplot

This code shows how to combine multiple line plot and contour plot with colorbar in one figure using Python and matplotlib.pyplot. To combine these plots, plt.subplots with gridspec_kw options are used. The xlims are also adjusted.

Python Matplotlib Tips: Combine three 2D colorap in one figure using Python and Matplotlib.pyplot

If you want to plot three 2D colormaps on one figure, the only feasible way is combining these three colormaps to one image. This can be achieved by corresponding three values to three primary elements, i.e. red, green, blue. However, interpretation of the completed figure is quite complex, and easiness of understanding for readers would be decreased. In my opinion, this way for plotting should be avoided.


In [1]:
import platform
print('python: '+platform.python_version())
import matplotlib.pyplot as plt
from matplotlib import __version__ as matplotlibversion
print('matplotlib: '+matplotlibversion)
import numpy as np
print('numpy: '+np.__version__)
%matplotlib inline
python: 3.6.3
matplotlib: 3.0.1
numpy: 1.15.4
In [2]:
x = np.linspace(-5, 5, 30)
y = np.linspace(-2, 2, 30)
xx, yy = np.meshgrid(x, y)
ts = np.linspace(-np.pi, np.pi, 5)
zz = [np.sin(xx+yy+t) for t in ts]
lvls = np.linspace(-1, 1, 30)
In [3]:
for i, (t, z) in enumerate(zip(ts, zz)):
    fig = plt.figure(figsize=(4, 3))
    ax1 = fig.add_subplot(1, 1, 1)
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    divider = make_axes_locatable(ax1)
    ax2 = divider.append_axes("right", size="5%", pad=0.05)
#     ax2 = fig.add_subplot(1, 2, 2)
    ctf = ax1.contourf(x, y, z, levels=lvls)
    cbar = plt.colorbar(ctf, cax=ax2)
    ctks = np.linspace(-1, 1, 5)
    ctkls = ["%.1f" % tk for tk in ctks]
    cbar.set_ticks(ctks)
    cbar.set_ticklabels(ctkls)
    cbar.set_label("z values")
    ax1.set_xlabel("x values")
    ax1.set_ylabel("y values")
    ax1.text(-4, -1.5, "$t\ =$ %.1f" % t, ha="left", bbox=dict({"facecolor": "w"}))
    plt.tight_layout()
    plt.savefig("frame_%02d.png" % i, bbox_inches="tight", pad_inches=0.02)

    # delete colorbar
    cbar.remove()
    plt.draw()
    plt.savefig("frame_%02d_wo_cbar.png" % i, bbox_inches="tight", pad_inches=0.02)

    # delete xticks and xlabel
    plt.setp(ax1.get_xticklabels(), visible=False)
    plt.xlabel("")
    plt.draw()
    plt.savefig("frame_%02d_wo_cbar_x.png" % i, bbox_inches="tight", pad_inches=0.02)
    
    if i == 0:
        ax2 = divider.append_axes("top", size="5%", pad=0.05)
    #     ax2 = fig.add_subplot(1, 2, 2)
        cbar = plt.colorbar(ctf, cax=ax2, orientation="horizontal")
        cbar.set_ticks(ctks)
        cbar.set_ticklabels(ctkls)
        cbar.set_label("z values")
        ax1.set_visible(False)
        ax2.xaxis.set_ticks_position('top') # set the position of the first axis to right
        ax2.xaxis.set_label_position('top') # set the position of the fitst axis to right

        plt.savefig("frame_%02d_cbar_only.png" % i, bbox_inches="tight", pad_inches=0.02)
        
    if i != len(ts) -1:
        # check the output figure when the iteration finishes
        plt.close()