Reputation: 3428
Cropping a picture with python and matplotlib seems easy (see this SO question). However, when cropping one figure in a plot with subplot, the overall size of the picture changes. I am pasting an example and the un-desired outcome:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('http://res.freestockphotos.biz/pictures/15/15912-illustration-of-a-banana-pv.png')
fig=plt.figure(figsize=(18, 4))
for i in range(1, 4):
fig.add_subplot(rows, columns, i)
plt.imshow(img)
if i > 2:
plt.imshow(img[:img.shape[0],:int(img.shape[1]/2)])
and here is the ugly result.
How can I have all pictures the same vertical size?
Upvotes: 1
Views: 4130
Reputation: 5774
The code you posted did not run for me. rows
and columns
were undefined. I ran:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('http://res.freestockphotos.biz/pictures/15/15912-illustration-of-a-banana-pv.png')
fig = plt.figure(figsize=(18, 4))
rows = 1 # I added this
columns = 3 # and this
for i in range(1, 4):
fig.add_subplot(rows, columns, i)
plt.imshow(img)
if i > 2:
plt.imshow(img[:img.shape[0], :int(img.shape[1] / 2)])
plt.show() # and this
With the result:
So I cannot reproduce the problem (and assume others cannot either). Perhaps this code solved your problem though? Best of luck!
After @ImportanceOfBeingErnest commented columns should be 6 I fiddled with it and maybe you're looking for the extent
setting? I ran
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('http://res.freestockphotos.biz/pictures/15/15912-illustration-of-a-banana-pv.png')
fig = plt.figure(figsize=(18, 4))
rows = 1
columns = 6
for i in range(1, 4):
fig.add_subplot(rows, columns, i)
if i > 2:
plt.imshow(img[:img.shape[0], :int(img.shape[1] / 2)], extent=(0, 50, 0, 50))
else:
plt.imshow(img, extent=(0, 50, 0, 50))
plt.tight_layout()
plt.show()
Yielding:
It basically just stretches the image to fit the extent
range you specify, which I believe is just the aspect ratio essentially. Would this be the desired effect to distort your image to fit the same size as the other images?
Upvotes: 1