我创建了一个包含4个子图(2 x 2)的图形,其中3个是imshow
类型,另一个是errorbar
。每个imshow
图表在其右侧还有一个颜色条。我想调整我的第三个图,调整图的区域正好在它上面的那个区域(带有颜色条)
作为例子(这就是我现在拥有的):
我怎么能调整第3个剧情?
此致
答案 0 :(得分:3)
要调整轴实例的尺寸,您需要使用set_position()方法。这也适用于subplotAxes。要获取轴的当前位置/尺寸,请使用get_position()方法,该方法返回Bbox实例。对我来说,概念上更容易与位置进行交互,即[左,下,右,顶]限制。要从Bbox访问此信息,请bounds属性。
在这里,我将这些方法应用于类似于上面示例的方法:
import matplotlib.pyplot as plt
import numpy as np
x,y = np.random.rand(2,10)
img = np.random.rand(10,10)
fig = plt.figure()
ax1 = fig.add_subplot(221)
im = ax1.imshow(img,extent=[0,1,0,1])
plt.colorbar(im)
ax2 = fig.add_subplot(222)
im = ax2.imshow(img,extent=[0,1,0,1])
plt.colorbar(im)
ax3 = fig.add_subplot(223)
ax3.plot(x,y)
ax3.axis([0,1,0,1])
ax4 = fig.add_subplot(224)
im = ax4.imshow(img,extent=[0,1,0,1])
plt.colorbar(im)
pos4 = ax4.get_position().bounds
pos1 = ax1.get_position().bounds
# set the x limits (left and right) to first axes limits
# set the y limits (bottom and top) to the last axes limits
newpos = [pos1[0],pos4[1],pos1[2],pos4[3]]
ax3.set_position(newpos)
plt.show()
您可能会觉得这两个图形看起来并不完全相同(在我的渲染中,左侧或xmin位置不是很正确),因此请随意调整位置,直到获得所需的效果。