Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set imshow scale

I'm fed up with matplotlib in that it's so hard to plot images in specified size.

I've two images in 32*32, 20*20 sizes. I just want to plot them in its original size, or in proportion to its original size.

After reading quite a few posts at SO, the code I'm using now is

plt.autoscale(False)
plt.subplot(1, 2, 1); 
plt.imshow(img_blob[0, 0, :, :], cmap='gray',
           interpolation='nearest', extent=[0, 32, 0, 32])
plt.subplot(1, 2, 2); 
plt.imshow(output_blob[0, 0, :, :], cmap='gray',
           interpolation='nearest', extent=[0, 20, 0, 20])
plt.show()

But the two images are still displayed in the same size.

enter image description here

I've tried figsize

plt.subplot(1, 2, 2, figsize=(2.0, 2.0)); 

But apparently there's no figsize attribute.

Is it possible to make the two subplots in different size?

like image 213
SolessChong Avatar asked Sep 21 '14 15:09

SolessChong


2 Answers

There is also an alternative to imshow you could use if the image proportions are very important ... there is something called figimage which displays a completely non-resampled image to your figure. Working with it is somewhat painstaking - it doesn't do much behind the scenes to make your life easier - but it can be useful for certain circumstances. Here is something using your example

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,1,20)
z1 = x[:,np.newaxis] * x
y = np.linspace(0,1,32)
z2 = y[:,np.newaxis] * y

fig = plt.figure(figsize=(2,1))
plt.figimage(z1, 25, 25, cmap='gray') # X, Y offsets are in pixels
plt.figimage(z2, 75, 25, cmap='gray')

plt.show()
like image 101
Ajean Avatar answered Sep 28 '22 23:09

Ajean


You can do differently sized plots using GridSpec. Since GridSpec requires creating equal-sized grid cells from the original figure rectangle, you would have to calculate the proportions yourself and then specifying cell spans in order to fill the correct space.

For your specific example:

import matplotlib.pyplot as plt
ax1 = plt.subplot2grid((3,6), (0,0), colspan=3, rowspan=3)
ax2 = plt.subplot2grid((3,6), (0,3), colspan=2, rowspan=2)

If you now plot using ax1 and ax2, it should keep the relative aspect ratios correct (given also a correct figure size). It should be possible to write up a method to do these calculations automatically, though.

like image 30
glopes Avatar answered Sep 28 '22 21:09

glopes