简体   繁体   English

matplotlib无花果大小与颜色条

[英]matplotlib fig size with colorbar

I'm trying to produce two figure. 我正在尝试产生两个数字。 The second one is equal to the first one, with the only exception that it has superimposed an image with the corresponding colorbar. 第二个与第一个相等,唯一的不同是第二个已将图像与相应的色条叠加在一起。 I need this in a presentation for a correct overlay. 我需要在演示文稿中获得正确的叠加层。 The code I'm using is the following 我正在使用的代码如下

import matplotlib as mpl
# first figure 
fig = mpl.pylab.figure(figsize=(10, 7))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot(x,y)
ax.set_xlabel(r'x')
ax.set_ylabel(r'y')
ax.set_xlim([0,1])
ax.set_ylim([0,1])
mpl.pylab.savefig('one.pdf',bbox_inches='tight')

# second figure
fig = mpl.pylab.figure(figsize=(10, 7))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot(x,y)
ax.set_xlabel(r'x')
ax.set_ylabel(r'y')
ax.set_xlim([0,1])
ax.set_ylim([0,1])
im = ax.imshow(image,aspect='auto',origin='lower',extent=(0,1,0.5,1))
cb = fig.colorbar(im, orientation='vertical')
cb.set_label(r'p$_e$ [Pa]', fontsize = 18)
mpl.pylab.savefig('two.pdf',bbox_inches='tight')

The problem is that I would like that the canvas (I think this is the correct name, ie the space occupied by the axis and label) to be exactly the same for the two figures, whereas the second one is shrink because of the colorbar. 问题是我希望两个图形的画布(我认为这是正确的名称,即轴和标签所占据的空间)完全相同,而第二个图形是由​​于颜色条而缩小的。 How can I correctly determine the size for the figures? 如何正确确定数字的大小?

Check out this post 看看这个帖子

You can also check out this example 您也可以查看此示例

I would recommend either plotting both plots on the same figure, making a grid, or doing a separate colorbar as in this example 我建议要么在同一图形上绘制两个图,要么制作一个网格,或者像本例中那样做一个单独的颜色条

import matplotlib as mpl
# first plot 
plt.subplot(131)
...
# second plot
plt.subplot(132)
...

#colorbar
plt.subplot(133)
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM