Draw three colormap with two colorbar using python and matplotlib.pyplot


The result is:

Draw three colormap with two colorbar using python and matplotlib.pyplot

This page shows an example of how to arrange three colormap with two colorbar. One colorbar corresponds two colormaps, while the other corresponds to one colormap. Gridspec enables us to properly adjust these positions in the figure.

See also:

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.

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 two contourf and two colorbar into one figure using Python and matplotlib.pyplot

This page shows how to combine two contourf and two colorbar into one figure using python and matplotlib.pyplot. The example is the axial symmetric model which has r-z coordinates. The z axis is the axis of symmetry. A charged sphere is placed at (r,z)=(0,0) and the electric potential and electric field is calculated and plotted into one figure. The right side of the figure is electric potential and the left side is electric field.


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

from matplotlib.colors import LinearSegmentedColormap
from matplotlib.gridspec import GridSpec, GridSpecFromSubplotSpec
python: 3.6.3
matplotlib: 3.0.1
numpy: 1.15.4
In [2]:
# define data to be plotted
xmn, xmx, xnn = -10, 10, 501
x = np.linspace(xmn, xmx, xnn)
ymn, ymx, ynn = 0, 20, 501
y = np.linspace(ymn, ymx, ynn)
xx, yy = np.meshgrid(x, y)
u = np.sin(xx)
v = np.sin(yy)-1
w = u+v
In [3]:
fig = plt.figure(figsize=(9, 3))

# Divide figure to grid.
# gs1 is a grid to draw u, v with one colorbar.
# gs2 is another grid to draw w with another colorbar.
gs1 = GridSpec(nrows=2, ncols=2, left=0.0, right=0.3, width_ratios=[0.2, 0.01], height_ratios=[1, 1])
gs2 = GridSpec(nrows=1, ncols=2, left=0.45, right=1.0, width_ratios=[0.4, 0.01])

# Assign axes from gs1 and gs2.
ax11 = fig.add_subplot(gs1[0, 0])
ax12 = fig.add_subplot(gs1[1, 0], sharex=ax11)
ax21 = fig.add_subplot(gs1[:, 1])
ax31 = fig.add_subplot(gs2[0])
ax41 = fig.add_subplot(gs2[1])

# Adjust grid
gs1.update(wspace=0.15, hspace=0.15) # set the spacing between axes. 
gs2.update(wspace=0.05) # set the spacing between axes. 
ax31.set_xlabel("x")
ax31.set_ylabel("y")
ax11.set_ylabel("y")
ax12.set_xlabel("x")
ax11.yaxis.set_label_coords(-0.17, 0)
plt.setp(ax11.get_xticklabels(), visible=False)

# plot contourf figure using u and v using gs1 area
lvls = np.linspace(min(u.min(), v.min()), max(u.max(), v.max()), 13)
cnt1 = ax11.contourf(xx, yy, u, cmap="jet", levels=lvls)
cnt2 = ax12.contourf(xx, yy, v, cmap="jet", levels=lvls)
cb1 = plt.colorbar(cnt1, cax=ax21)
cb1.set_label("u, v [a.u.]")

# plot contourf figure using w to the gs2 area
cnt2 = ax31.contourf(xx, yy, w, cmap="plasma")
cb2 = plt.colorbar(cnt2, cax=ax41)
cb2.set_label("w = u+v [a.u.]")

# Save the figure
plt.savefig('combine_multiple_colormap_in_one_figure.png', bbox_inches='tight', pad_inches=0.02, dpi=150)