Eagle
Eagle

Reputation: 3486

changing size of a plot in a subplot figure

i create a figure with 4 subplots (2 x 2), where 3 of them are of the type imshow and the other is errorbar. Each imshow plots have in addition a colorbar at the right side of them. I would like to resize my 3rd plot, that the area of the graph would be exactly under the one above it (with out colorbar)

as example (this is what i now have):

example

How could i resize the 3rd plot?

Regards

Upvotes: 1

Views: 4966

Answers (1)

Yann
Yann

Reputation: 35573

To adjust the dimensions of an axes instance, you need to use the set_position() method. This applies to subplotAxes as well. To get the current position/dimensions of the axis, use the get_position() method, which returns a Bbox instance. For me, it's conceptually easier to just interact with the position, ie [left,bottom,right,top] limits. To access this information from a Bbox, the bounds property.

Here I apply these methods to something similar to your example above:

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()

You may feel that the two plots do not exactly look the same (in my rendering, the left or xmin position is not quite right), so feel free to adjust the position until you get the desired effect.

Upvotes: 5

Related Questions