你可能想看看Matplotlib的
ImageGrid
:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
from matplotlib.colors import Normalize
from matplotlib.cm import ScalarMappable
x, y = np.mgrid[-2:2:100j, -2:2:100j]
z1 = np.sin(x**2 + y**2)
z2 = np.cos(x**2 + y**2)
cmap = "inferno"
fig = plt.figure()
grid = ImageGrid(
fig, 111, # similar to subplot(111)
nrows_ncols=(1, 2), # creates 1x2 grid of Axes
axes_pad=0.2, # pad between Axes in inch.
cbar_mode="single"
)
grid[0].contourf(x, y, z1, cmap=cmap)
grid[1].contourf(x, y, z2, cmap=cmap)
norm = Normalize(vmin=min(z1.min(), z2.min()), vmax=max(z1.max(), z2.max()))
cb2 = grid[1].cax.colorbar(
ScalarMappable(norm=norm, cmap=cmap),
label = "z value"
)
plt.show()