在matplotlib的图像网格上的两个colorbars [英] Two colorbars on image grid in matplotlib
问题描述
我想在matplotlib中使用以下布局:
I would like to have the following layout in matplotlib:
图像1a图像1b图像2a图像2b Colorbar a Colorbar b
Image 1a Image 1b Image 2a Image 2b Colorbar a Colorbar b
Colorbar a用于图像集a,Colobar b用于图像集b。
The Colorbar a is for Image set a, and the Colobar b is for Image set b.
我试图使用ImageGrid创建轴对于图像,但没有运气使彩色棒正确。例如:
I have tried to use ImageGrid to create the axes for images, but no luck in making the colorbars right. For example:
fig = plt.figure()
grid = ImageGrid(fig, 111, (1,6), aspect=False, share_all=False)
# Get data1a, data1b, ...
im1a = grid[0].pcolormesh(data1a)
im1b = grid[1].pcolormesh(data1b)
im2a = grid[2].pcolormesh(data2a)
im2b = grid[3].pcolormesh(data2b)
plt.colorbar(im1a, cax=grid[4])
plt.colorbar(im1b, cax=grid[5])
这个问题是对colorbar()的调用搞乱了图像的轴限制,即使我在ImageGrid()中指定了share_all = False。
The problem with this is that the calls to colorbar() messed up with the axis limits of the images even though I specified share_all=False in ImageGrid().
这有什么提示吗?非常感谢。
Is there any tip on this? Very much appreciated.
推荐答案
为了将来参考,有一个完整的工作示例,以便有人可以复制和粘贴你的编码并直接重现您的问题。例如,我可以看到你导入了 ImageGrid
,但完整的import语句会对此有所帮助,就像为 data1a <创建假数据集一样/ code>,
data1b
等。
For future reference, it helps to have a full working example, so that someone could copy and paste your code and reproduce your issue directly. For example, I can see you've imported ImageGrid
, but a full import statement would help with this, as would creating fake data sets for data1a
, data1b
, etc.
此外,看起来你有一个(1,6)
您上面的语句中应该有(1,4)
: grid = ImageGrid(图111,(1,4),aspect = False,share_all = False)
,虽然这不是你问题的解决方案。
Also, it looks like you have a (1,6)
where you should have (1,4)
in your statement above: grid = ImageGrid(fig, 111, (1,4), aspect=False, share_all=False)
, though this is not the solution to your problem.
当我需要两个或更多颜色条时,我的方法通常是在轴上使用 get_position()
,它返回轴角的坐标作为属性 X0,Y0,X1,Y1
。从这里开始,我分别定义每个颜色条的轴,并将它们精确地放置在我想要的位置。为了满足您的需求,您必须修补 fig.add_axes([1.01,bbox_ax.y0,0.02,bbox_ax.y1-bbox_ax.y0])$ c的详细信息$ c>在下面的代码中。例如,前两个条目
1.01,bbox_ax.y0
表示将底角放在 x = 1.01
和 Y = bbox_ax.y0
。后两个条目 0.02,bbox_ax.y1-bbox_ax.y0
分别定义颜色条轴的水平和垂直宽度。我喜欢颜色条轴与绘图轴齐平,所以我使用 bbox_ax.y1-bbox_ax.y0
作为垂直宽度。
When I want two or more color bars, my approach is typically to use get_position()
on an axis, which returns the coordinates for the axis corners as attributes x0,y0,x1,y1
. From here, I define each colorbar's axis separately and place each precisely where I want it to go. To get this to suit your needs, you'll have to tinker with the details of fig.add_axes([1.01, bbox_ax.y0, 0.02, bbox_ax.y1-bbox_ax.y0])
in the code below. For example, the first two entries 1.01, bbox_ax.y0
mean "place the bottom corner at x=1.01
and y=bbox_ax.y0
". The second two entries, 0.02, bbox_ax.y1-bbox_ax.y0
define the horizontal and vertical width of the colorbar axis, respectively. I like the colorbar axes to be flush with the plot axes, so I use bbox_ax.y1-bbox_ax.y0
for the vertical width.
请注意,我使用 mp.subplots()
而不是 ImageGrid()
,因为我对后者并不熟悉,我认为没必要。
Note that I'm using mp.subplots()
instead of ImageGrid()
, since I'm not as familiar with the latter, and I don't think it's necessary.
import matplotlib.pyplot as mp
import numpy
import mpl_toolkits.axes_grid1
data1a = numpy.random.rand(100,100)
data1b = numpy.random.rand(100,100)
data2a = numpy.random.rand(100,100)
data2b = numpy.random.rand(100,100)
fig, axes = mp.subplots(1, 4, figsize=(8,2))
im1a = axes[0].pcolormesh(data1a, cmap='magma')
im1b = axes[1].pcolormesh(data1b, cmap='magma')
im2a = axes[2].pcolormesh(data2a, cmap='viridis')
im2b = axes[3].pcolormesh(data2b, cmap='viridis')
fig.tight_layout()
# get bounding box information for the axes (since they're in a line, you only care about the top and bottom)
bbox_ax = axes[0].get_position()
# fig.add_axes() adds the colorbar axes
# they're bounded by [x0, y0, x_width, y_width]
cbar_im1a_ax = fig.add_axes([1.01, bbox_ax.y0, 0.02, bbox_ax.y1-bbox_ax.y0])
cbar_im1a = mp.colorbar(im1a, cax=cbar_im1a_ax)
cbar_im2a_ax = fig.add_axes([1.09, bbox_ax.y0, 0.02, bbox_ax.y1-bbox_ax.y0])
cbar_im1a = mp.colorbar(im2a, cax=cbar_im2a_ax)
这产生如下数字:
您也可以使用略有不同语法的2x2网格执行此操作:
You can also do this as a 2x2 grid with slightly different syntax:
fig, axes = mp.subplots(2, 2, figsize=(4,4))
im1a = axes[0,0].pcolormesh(data1a, cmap='magma')
im1b = axes[0,1].pcolormesh(data1b, cmap='magma')
im2a = axes[1,0].pcolormesh(data2a, cmap='viridis')
im2b = axes[1,1].pcolormesh(data2b, cmap='viridis')
fig.tight_layout()
bbox_ax_top = axes[0,1].get_position()
bbox_ax_bottom = axes[1,1].get_position()
cbar_im1a_ax = fig.add_axes([1.01, bbox_ax_top.y0, 0.02, bbox_ax_top.y1-bbox_ax_top.y0])
cbar_im1a = mp.colorbar(im1a, cax=cbar_im1a_ax)
cbar_im2a_ax = fig.add_axes([1.01, bbox_ax_bottom.y0, 0.02, bbox_ax_bottom.y1-bbox_ax_bottom.y0])
cbar_im1a = mp.colorbar(im2a, cax=cbar_im2a_ax)
产生这个数字:
这篇关于在matplotlib的图像网格上的两个colorbars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!