多个热图子图:相同的 Heatbar
Multiple Heatmap Subplots: Same Heatbar
这是我绘制热图的方式:
import matplotlib.pyplt as plt
ax = plt.gca()
im = ax.imshow(values)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size='5%', pad=0.05)
plt.colorbar(im, cax=cax)
现在我想创建一个 2x2 子图,有 4 个不同的热图,并且都具有相同的热条。我对如何实现这一目标一无所知,并希望任何朝着正确方向的推动。
您可以使用 AxesGrid
from mpl_toolkits.axes_grid1
. See the example here 执行此操作(具体来说,查看该示例中的函数 demo_grid_with_single_cbar
)。
我稍微修改了那个例子,以适应你的 2x2 网格,右边有颜色条。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
# Some random data
values1 = np.random.rand(10,10)
values2 = np.random.rand(10,10)
values3 = np.random.rand(10,10)
values4 = np.random.rand(10,10)
vals = [values1,values2,values3,values4]
fig = plt.figure()
grid = AxesGrid(fig, 111,
nrows_ncols=(2, 2),
axes_pad=0.05,
share_all=True,
label_mode="L",
cbar_location="right",
cbar_mode="single",
)
for val, ax in zip(vals,grid):
im = ax.imshow(val, vmin=0, vmax=1)
grid.cbar_axes[0].colorbar(im)
for cax in grid.cbar_axes:
cax.toggle_label(False)
plt.show()
这是我绘制热图的方式:
import matplotlib.pyplt as plt
ax = plt.gca()
im = ax.imshow(values)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size='5%', pad=0.05)
plt.colorbar(im, cax=cax)
现在我想创建一个 2x2 子图,有 4 个不同的热图,并且都具有相同的热条。我对如何实现这一目标一无所知,并希望任何朝着正确方向的推动。
您可以使用 AxesGrid
from mpl_toolkits.axes_grid1
. See the example here 执行此操作(具体来说,查看该示例中的函数 demo_grid_with_single_cbar
)。
我稍微修改了那个例子,以适应你的 2x2 网格,右边有颜色条。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
# Some random data
values1 = np.random.rand(10,10)
values2 = np.random.rand(10,10)
values3 = np.random.rand(10,10)
values4 = np.random.rand(10,10)
vals = [values1,values2,values3,values4]
fig = plt.figure()
grid = AxesGrid(fig, 111,
nrows_ncols=(2, 2),
axes_pad=0.05,
share_all=True,
label_mode="L",
cbar_location="right",
cbar_mode="single",
)
for val, ax in zip(vals,grid):
im = ax.imshow(val, vmin=0, vmax=1)
grid.cbar_axes[0].colorbar(im)
for cax in grid.cbar_axes:
cax.toggle_label(False)
plt.show()