matplotlib axesgrid - additional colorbar?

拟墨画扇 提交于 2021-01-27 21:12:22

问题


I want to add another colorbar to a plot where I use AxesGrid toolkit. For example, I add a colorbar axes using ImageGrid on the left, and then I add another one on the right manually. Here is a simple example:

f = plt.figure(1)
grid = ImageGrid(f, 111,  # similar to subplot(111)
                 nrows_ncols=(2, 2),
                 axes_pad=0.01,
                 add_all=True,
                 cbar_location="left",
                 label_mode='L',
                 cbar_mode="edge",
                 cbar_size="3%",
                 cbar_pad="2%",
                 )
for i in range(3):
    m = grid[i].matshow(np.arange(100).reshape((10, 10)))
plt.colorbar(m, grid.cbar_axes[0])
m = grid[3].matshow(np.arange(100).reshape((10, 10)), cmap='plasma')
plt.colorbar(m, shrink=0.5, anchor=(0, 0))
plt.show()

How do I make the new colorbar match the position of one of the subplots in the grid exactly? I at least managed to fix the size and y-position using shrink and anchor... But it also gets a bit complicated if I try to account for the padding between subplots, and if they are rectangular rather than square...


回答1:


One option is to manually place the colorbar axes according to the position of one of the axes. To this end one first needs to draw the canvas, such that the positions are known. One can then create a new axes according to coordinates of image plot. This new axes will serve as the colorbar axes.

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import ImageGrid

fig = plt.figure(1)
grid = ImageGrid(fig, 111,  # similar to subplot(111)
                 nrows_ncols=(2, 2),
                 axes_pad=0.01,
                 add_all=True,
                 cbar_location="left",
                 label_mode='L',
                 cbar_mode="edge",
                 cbar_size="3%",
                 cbar_pad="2%",
                 )
for i in range(3):
    m = grid[i].matshow(np.arange(100).reshape((10, 10)))
plt.colorbar(m, grid.cbar_axes[0])
m = grid[3].matshow(np.arange(100).reshape((10, 10)), cmap='plasma')

# first draw the figure, such that the axes are positionned
fig.canvas.draw()
#create new axes according to coordinates of image plot
trans = fig.transFigure.inverted()
g3 =grid[3].bbox.transformed(trans)
pos = [g3.x1 + g3.width*0.02, g3.y0, g3.width*0.03, g3.height ]
cax = fig.add_axes(pos) #l,b,w,h
# add colorbar to new axes
plt.colorbar(m, cax=cax)


plt.show()

This method depends on the position of the axes in the figure, once that changes, e.g. because the figure is rezised, unforseable things might happen.

A different method, which does not rely on the drawn coordinates, is to (mis)use inset axes and place the inset outside the axes. In this way the coordinates by which the inset is located are axes coordinates, so the colorbar will change its position according to the axes.

from mpl_toolkits.axes_grid1.inset_locator import inset_axes

cax = inset_axes(grid[3], "3%", "100%", loc=3, bbox_to_anchor=(1.02,0,1,1), 
                 bbox_transform=grid[3].transAxes, borderpad=0.0)
plt.colorbar(m, cax=cax)


来源:https://stackoverflow.com/questions/43306001/matplotlib-axesgrid-additional-colorbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!