1

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.

enter image description here

How can I have all pictures the same vertical size?

famargar
  • 3,258
  • 6
  • 28
  • 44

1 Answers1

1

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:

output

So I cannot reproduce the problem (and assume others cannot either). Perhaps this code solved your problem though? Best of luck!

update

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:

new output

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?

Community
  • 1
  • 1
Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • 2
    To reproduce choose `rows=1; columns = 6`. – ImportanceOfBeingErnest Apr 03 '19 at 20:12
  • OK I would love to close my own question as it is a result of previous variables left in memory (rows, columns) but I can't. One issue is pending though - why the third banana has an offset? I guess matplotlib subplot formats according to the center of the picture, not the leftmost part? – famargar Apr 03 '19 at 20:17
  • My desired behaviour was to be accomplished by anchoring the plots at the westmost point ax = fig.add_subplot(1, 3,i); ax.set_anchor('W'). So the main point of my question was that I an old value of columns was in memory (playing with jupyter notebook, classic mistake). I suggest you guys close my question as I cannot – famargar Apr 03 '19 at 20:34
  • 1
    Your question received an answer which was upvoted and accepted. Also the question itself is upvoted. You cannot delete a question that has a positive score or where an answer has a positive score. A moderator could delete them, but they wouldn't do that usually, given that at least one person found this Q&A valuable. – ImportanceOfBeingErnest Apr 03 '19 at 22:46