如何在pyplot(matplotlib)的同一图上添加两个颜色条?
问题描述:
我想绘制一个子图,如:
I would like to plot one subplot like:
Title 1
Fig1 Fig2 Fig3
为这3个数字(1、2、3)提供了一个通用的颜色条.
With a common colorbar for these 3 figures (1,2,3).
Title2
Fig4 Fig5 Fig6
为这3个数字(4、5、6)提供了一个通用的颜色条.
With a common colorbar for these 3 figures (4,5,6).
如上所述,我还没有找到在同一张图上添加两个不同颜色条的方法.
I haven't found a way to add two different colorbars on the same figure as described above.
答
这有些棘手,但是您可以使用通用的颜色栏共享多组子图.
It's a bit tricky, but you can share sets of subplots with a common colorbar.
我已经借鉴了一些以前可能值得一读的答案:
I've drawn on a few previous anwers that might be worth reading as well:
当然,对于matplotlib,文档:
And of course, the documentation for matplotlib:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
# Generate some random data
data_top = np.random.random((3,10,10)) * 5
data_bot = np.random.random((3,20,20)) * 10
fig = plt.figure()
grid_top = ImageGrid(fig, 211, nrows_ncols = (1, 3),
cbar_location = "right",
cbar_mode="single",
cbar_pad=.2)
grid_bot = ImageGrid(fig, 212, nrows_ncols = (1, 3),
cbar_location = "right",
cbar_mode="single",
cbar_pad=.2)
for n in xrange(3):
im1 = grid_top[n].imshow(data_top[n],
interpolation='nearest', vmin=0, vmax=5)
im2 = grid_bot[n].imshow(data_bot[n], cmap=plt.get_cmap('bone'),
interpolation='nearest', vmin=0, vmax=10)
grid_top.cbar_axes[0].colorbar(im1)
grid_bot.cbar_axes[0].colorbar(im2)
plt.show()