如何在pyplot中调整两个子图的大小,一个带有彩条,另一个不带彩条? [英] How to adjust size of two subplots, one with colorbar and another without, in pyplot ?

查看:47
本文介绍了如何在pyplot中调整两个子图的大小,一个带有彩条,另一个不带彩条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此示例

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

plt.subplot(121)
img = plt.imshow([np.arange(0,1,.1)],aspect="auto")
ax = plt.gca()
divider = make_axes_locatable(ax)
cax = divider.append_axes("bottom", size="3%", pad=0.5)
plt.colorbar(img, cax=cax, orientation='horizontal')
plt.subplot(122)
plt.plot(range(2))
plt.show()

我想让这两个图形(没有颜色条的绘图区域)具有相同的大小.

I want to make these two figures (plot region without colorbar) of the same size.

如果垂直绘制颜色条或使用两行(211,212)而不是两列,则会自动调整大小.

The size is automatically adjusted if the colorbar is plotted vertically or if two rows are used (211, 212) instead of two columns.

推荐答案

对于第二个子图,基本上可以对第一个子图执行相同的操作,即创建一个分隔符并附加一个具有相同参数的轴,只是在这种情况下,我们不希望轴中有颜色条,而是简单地关闭轴.

One can basically do the same for the second subplot as for the first, i.e. create a divider and append an axes with identical parameters, just that in this case, we don't want a colorbar in the axes, but instead simply turn the axis off.

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

ax = plt.subplot(121)
img = ax.imshow([np.arange(0,1,.1)],aspect="auto")
divider = make_axes_locatable(ax)
cax = divider.append_axes("bottom", size="3%", pad=0.5)
plt.colorbar(img, cax=cax, orientation='horizontal')

ax2 = plt.subplot(122)
ax2.plot(range(2))
divider2 = make_axes_locatable(ax2)
cax2 = divider2.append_axes("bottom", size="3%", pad=0.5)
cax2.axis('off')
plt.show()

这篇关于如何在pyplot中调整两个子图的大小,一个带有彩条,另一个不带彩条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆