I am using AxesGrid in matplotlib to create a plot that overlays two imshow plots, with a separate colourbar side by side for each image colourmap. While I can see in this question/answer that using pyplot.colorbar() automatically plots the second colour bar next to the first, this doesn't seem to work with AxesGrid.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
fig = plt.figure(1, facecolor='white',figsize=(10,7.5))
grid = AxesGrid(fig, 111,
nrows_ncols=(1, 1),
axes_pad=(0.45, 0.15),
label_mode="1",
share_all=True,
cbar_location="right",
cbar_mode="each",
cbar_size="7%",
cbar_pad="2%",
)
im = grid[0].imshow(my_image, my_cmap)
cbar = grid.cbar_axes[0].colorbar(im)
im2 = grid[0].imshow(my_image_overlay, my_cmap2, alpha=0.5)
cbar2 = grid.cbar_axes[0].colorbar(im2)
plt.show()
However, this just shows the second colourbar. (Presumably overlaying the first one). I tried overriding the padding in cbar2 with:
cbar2 = grid.cbar_axes[0].colorbar(im2, pad=0.5)
but this results in an error with "got an unexpected keyword argument 'pad'"
Is there a way to offset the second colourbar?
I think you may need to make two axes for the colorbars and use them:
from mpl_toolkits.axes_grid1 import make_axes_locatable
# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
'cause you're currently using the same allocated space for both.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.