[英]matplotlib change colorbar height within own axes
I'm currently trying to create a stackplot of graphs, of which my first two have colorbars. 我目前正在尝试创建图形堆栈图,其中的前两个具有颜色条。 To do this nicely, I'm using GridSpec to define two columns, with the second being much thinner and specifically for colorbars (or other out-of-plot things like legends).
为了很好地做到这一点,我使用GridSpec定义了两列,第二列要细得多,专门用于颜色条(或其他图例等图例)。
grids = gs.GridSpec(5, 2, width_ratios=[1, 0.01])
ax1 = fig.add_subplot(grids[0, 0])
cax1 = fig.add_subplot(grids[0, 1])
The problem is that for these top two plots, the ticklabels of my colorbar overlap slightly, due to the fact that I've got zero horizontal space between my plots. 问题在于,对于这前两个图,由于我的图之间的水平空间为零,因此我的颜色栏的刻度标签略有重叠。
I know that there are ways to control the height of the colorbar, but they seem to rely on the colorbar making it's own axes by borrowing space from the parent axes. 我知道有多种方法可以控制颜色条的高度,但是它们似乎依靠颜色条通过从父轴借用空间使其成为自己的轴。 I was wondering if there was any way to control how much space (or specifically, height) the colorbar takes up when you use the cax kwarg
我想知道当您使用cax kwarg时是否有任何方法可以控制彩条占用多少空间(或具体地说,高度)?
fig.colorbar(im1, cax=cax1, extend='max')
or if it defaults (immutably) to take up the entire height of the axes given to it. 或默认(不可变地)占用指定给它的轴的整个高度。
Thanks! 谢谢!
EDIT: Here's an image of the issue I'm struggling with. 编辑: 这是我正在努力解决的问题的图像。
If I could make the second slightly shorter, or shift the upper one slightly up then it wouldn't be an issue. 如果我可以将第二个稍短一些,或者将第二个稍稍向上移动一点也不是问题。 Unfortunately since I've used GridSpec (which has been amazing otherwise) I'm constrained to the limits of the axes.
不幸的是,由于我使用了GridSpec(否则就太神奇了),我受到了轴的限制。
I don't think there is any way to ask colorbar to not fill the whole cax
. 我认为没有任何方法可以让
cax
不能填满整个cax
。 However, it is fairly trivial to shrink the size of the cax
before (or after actually) plotting the colorbar. 但是,在绘制
cax
之前(或之后)缩小cax
的大小相当简单。
I wrote this small function: 我写了这个小函数:
def shrink_cbar(ax, shrink=0.9):
b = ax.get_position()
new_h = b.height*shrink
pad = (b.height-new_h)/2.
new_y0 = b.y0 + pad
new_y1 = b.y1 - pad
b.y0 = new_y0
b.y1 = new_y1
ax.set_position(b)
which can be used like so: 可以这样使用:
fig = plt.figure()
grids = gs.GridSpec(2, 2, width_ratios=[1, 0.01])
ax1 = fig.add_subplot(grids[0, 0])
cax1 = fig.add_subplot(grids[0, 1])
ax2 = fig.add_subplot(grids[1, 0])
cax2 = fig.add_subplot(grids[1, 1])
shrink_cbar(cax2, 0.75)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.